"并使用对象调用方法,创建若干学生并显示他们的数据!"这句话我点问题哦,创建对象一般不是调用方法来创建的。是直接用构造方法来new 的。下面这个程序可以说明从一个类中调用另一个类的方法。
class Student {
int studentNo ;
String studentName;
public void setStudent (int studenteNo, String studentName) {
this.studentName = studentName;
this.studentNo = studenteNo;
}
public void show () {
System.out.println("学号:" + this.studentNo + " 姓名:" + this.studentName + "\n");
}
}
public class Test{
public static void main(String[] args) {
Student st1 = new Student ();
st1.setStudent(1,"小明");
Student st2 = new Student ();
st2.setStudent(2,"小强");
st1.show();
st2.show();
}
}
public class testStudent {
public static void main(String[] args) {
// TODO Auto-generated method stub
Student s=new Student();
s.setName("张三");
s.setAge(20);
System.out.println(s);
Student s1=new Student("王五",21);
System.out.println(s1);
}
}
class Student {
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Student() {
}
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "学生的姓名是\t"+name+"学生的年龄是\t"+age;
}
}
1,二楼说的说的都对。想比较,二楼看起来更舒服,明白点!
太简单