日期:2014-05-20  浏览次数:20661 次

还没入门,所以这个问题我也不知道该怎么问
public class TestExtends {
public static void main(String[] args) {
w.setName("wangming");
System.out.println(w.name);
}
}


 class Person {
String name;
String age;

public void setName(String _name,String _age) {
this.name = _name;

}
}

class Studens extends Person {
private String school;
Studens(){};

public void setSchool(String _school) {
school = _school;
}
String getSchool(){
return school;
}
Studens w = new Studens();
}
我想在主方法里面 打印 w的名字 编译说找不到符号 弄了半天也不知道怎么解决

------解决方案--------------------


public class TestExtends {
public static void main(String[] args) {
Studens w = new Studens();//在主方法里新建student对象w,否则你新建的w,放在student类里,只有这个Student类认识
w.setName("wangming","12");//这里的方法设置参数应与你定义的参数对应
System.out.println(w.name);
}
}

class Person {
String name;
String age;

public void setName(String _name, String _age) {
this.name = _name;

}
}

class Studens extends Person {
private String school;

Studens() {
};

public void setSchool(String _school) {
school = _school;
}

String getSchool() {
return school;
}


}
换成这段代码,
------解决方案--------------------
Java code
package ttt;

public class TestExtends {
    public static void main(String[] args) {
    Studens w = new Studens();
    w.setName("wangming", null);//你定义的set方法用了俩个参数
    System.out.println(w.name);
    }
}

class Person {
    String name;
    String age;

    public void setName(String _name, String _age) {
    this.name = _name;

    }
}

class Studens extends Person {
    private String school;

    Studens() {
    };

    public void setSchool(String _school) {
    school = _school;
    }

    String getSchool() {
    return school;
    }

}