Java:设置和获取字符串的方法

时间:2015-06-08 23:14:43

标签: java class object

我必须编写一个程序,它使用适当的get和set方法输出以下内容: 这个人是John Smith(21岁,男性)

到目前为止,我到达了这里:

public class ListNode {
    Node head = null;

    int nodeCount= 0;

    int counter = 0;

    ListNode(){
        head = null;

    }

    public void insertNode( String name ) {

        if (head == null) {
            head = new Node(name, null);
            nodeCount++;
        } else {
            Node temp = new Node(name, null);
            temp.next = head;
            head = temp;


            nodeCount++;
        }
    }
        public Node reverseTest(Node L){

            // Node current = new Node(null,null);

            if(L == null || L.next ==null){
                return L;
            }

            Node remainingNode =  reverseTest(L.next);
            Node cur = remainingNode;
            while(cur.next !=null){
                cur=cur.next;
            }

            L.next = null;
            cur.next = L;

            return  remainingNode;

        }


    public static void main(String[] args){

        ListNode newList = new ListNode();
        newList.insertNode("First");
        newList.insertNode("Second");
        newList.insertNode("Third");
        newList.insertNode("Fourth");

        newList.reverseTest(newList.head);


    }
}

}

在主要课程中:

public class Person {

    private int age;
    private String name;
    private String gender;

    public void setAge(int age){

        this.age= age;
    }

    public int getAge(){

        return age;
    }

    public void setName(String thename){

        this.name=name;
    }

    public String getName(){
        return name;
    }

    public void setGender(String gender){
        this.gender= gender;
    }

    public String getGender(){

        return gender;
    }

    public void person(){

        System.out.printf("This person is %s(%d, %s)", getName(), getAge(), getGender());
    }

问题是我收到的错误如方法setName(String)未定义为主要的set方法类型Person 。 我仍然是Java的初学者,所以我还没有掌握它。

1 个答案:

答案 0 :(得分:3)

我看到的唯一问题是setName本身的实现。您根本不使用输入变量thename

public void setName(String thename){

    this.name=name;
}

最后一行应该是

this.name = thename;

但这不会给你你说的错误(setName不存在)。我猜你要么全部用小写字母定义实际方法(比如public void setname(String thename)),要么我们没有看到所有的代码。