Java继承的实现
继承的实现
1.继承的基本概念
扩展父类的功能
2.Java中使用extends关键字完成继承。
示例代码:
public class ExtendsDemo01 {
static class Person{
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void tell(){
System.out.println("姓名: "+getName()+" 年龄:"+getAge());
}
}
static class Student extends Person{
private int score;
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public void say(){
System.out.println("成绩:"+getScore());
}
}
public static void main(String [] args){
Student s=new Student();
s.setAge(20);
s.setName("张三");
s.setScore(100);
s.tell();
s.say();
}
}