子类继承父类,再调用子类
程序员 - 子类
继承自人类
在人类的基础上进行扩展 ```java package Test12_Demo.Demo03;/*
@create 2020—11—30—10:41 */
import Test12_Demo.Demo03.Person;
public class Coder extends Person {
public String name;
public void Coding() {
System.out.println("Hello Word!");
}
}
父类:
```java
package Test12_Demo.Demo03;/*
@create 2020--11--30--10:40
*/
public class Person {
private String name;
private int age;
public Person() {
}
public void eat() {
System.out.println("one day day only just eat");
}
public void sleep() {
System.out.println("sleeping");
}
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;
}
}
调用子类:
package Test12_Demo.Demo03;/*
@create 2020--11--30--10:29
*/
import Test12_Demo.Demo03.Coder;
public class ExtendsDemo {
public static void main(String[] args) {
//创建子类对象
Coder c = new Coder();
//子类继承了父类的属性 - 只能通过get/set方法处理
c.setName("詹姆斯.高斯林");
c.setAge(65);
System.out.println(c.getName() + "," + c.getAge());
//子类继承了父类的方法
c.eat();
c.sleep();
//子类访问自己的方法
c.Coding();
}
}