1. 程式人生 > 其它 >Java:面向物件-高階篇(上)

Java:面向物件-高階篇(上)

技術標籤:JAVA/JSP

1,繼承

1.1,繼承的基本概念

class Person{
    private String name;
    private int 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;}
}
class Student extends Person{
    private String school;
    public String getSchool() {return school;}
    public void setSchool(String school) {this.school = school;}
}
public class HelloWord {
    public static void main(String[] args) {
        Student student = new Student();
        student.setName("燕雙嚶");
        student.setAge(30);
        student.setSchool("NUDT");
        System.out.println("姓名:"+student.getName()+",年齡:"+student.getAge()+",學校:"+student.getSchool());
    }
}

在Java中只允許單繼承,不能使用多程序,即一個子類只能繼承一個父類。但是允許程序多層繼承,即一個子類可以有一個父類,一個父類還可以有一個父類。