/*子类对象的实例化过程*/
class Person13{
private String name;
private int age;
public Person13(){
System.out.println("父类对象进行了实例化");
}
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 void prin(){
System.out.println("名字:"+name+", 年龄:"+age);
}
}
class Student2 extends Person13{
private String school;
public Student2(){
System.out.println("子类对象进行了实例化");
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public void pri(){
System.out.println("学校:"+school);
}
}
public class ExtendsDemo2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Student2 st = new Student2();
st.setName("张三");
st.setAge(35);
st.setSchool("清华大学");
st.prin();
st.pri();
}
}
class Person13{
private String name;
private int age;
public Person13(){
System.out.println("父类对象进行了实例化");
}
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 void prin(){
System.out.println("名字:"+name+", 年龄:"+age);
}
}
class Student2 extends Person13{
private String school;
public Student2(){
System.out.println("子类对象进行了实例化");
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public void pri(){
System.out.println("学校:"+school);
}
}
public class ExtendsDemo2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Student2 st = new Student2();
st.setName("张三");
st.setAge(35);
st.setSchool("清华大学");
st.prin();
st.pri();
}
}

本文通过一个具体的Java示例展示了如何实现子类继承父类,并完成子类对象的实例化过程。具体包括定义父类Person13及子类Student2,子类中不仅继承了父类的属性和方法,还新增了特定属性和方法。
884

被折叠的 条评论
为什么被折叠?



