Calling exercise of construction method in word parent class

Person class: (parent class)

Public class person {private string name; private int age; public person() {this ("Li Si", 20);} public Person(String name,int age){ this.name=name; this.age=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; }

}

Student class: (subclass)

public class Student extends Person { public Student(String name,int age){ super(name,age); } public Student(){ }

}

Worker class: (subclass)

public class Worker extends Person { public Worker(String name,age); } public Worker(){ }

}

Test class:

Public class test {public static void main (string [] args) {person P = new person ("Zhang San", 23); system.out.println (p.getname()); system.out.println (p.getage()); student s = new student(); system.out.println (s.getage()); system.out.println (s.getname()); worker w = new worker ("Wang Wu", 27); system.out.println (w.getname()); system.out.println (w.getage()) ); } }

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>