package 继承;
class Person{
public Person() {
System.out.println("Person 构造方法");
}
private String name;
private int age;
public void setname(String name) {
this.name = name;
}
public void setage(int age) {
this.age = age;
}
public String getname() {
return this.name;
}
public int getage() {
return this.age;
}
}
class Student extends Person{
public Student() {
super();
}
// public Student() {
// System.out.println("Student 构造方法");
// }
private String SchoolName;
public void setSchoolName(String SchoolName){
this.SchoolName = SchoolName;
}
public String getSchoolString() {
return this.SchoolName;
}
}
public class TestDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Student p1 = new Student();
p1.setname("RICK");
p1.setage(10);
p1.setSchoolName("Bilibili");
System.out.println("Name: "+p1.getname()+", "+"Age: "+p1.getage());
System.out.println("School: "+ p1.getSchoolString());
}
}
[JAVA]继承与构造
最新推荐文章于 2024-07-14 22:26:46 发布