2019-07-31 Person与Student

package com.foreknow.extend;
/**
 * 继承(extends):
 * Student子类        Person父类
 * 子类会继承父类所有的属性和方法
 * 注意:
 * 1.Java只继承单继承
 * 2.所有类默认都会继承Object类 public class Person extends Object
 * @author Administrator
 *
 */
public class Person{
    private String id;
    private String name;
    private int age;
    
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    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;
    }
    //重写了Object中的toString方法
    public String toString(){
        return "随便";
    }
}

package com.foreknow.extend;

public class Student extends Person{
    private String school;//所在学校
    public String getSchool() {
        return school;
    }
    public void setSchool(String school) {
        this.school = school;
    }
    
    public void showMessage(){
        System.out.println("showMessage......"+this.getId()+"-"+this.getName()+"-"+this.getSchool());
    }
    
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.setId("100");
        s1.setName("aaa");
        s1.setAge(10);
        s1.setSchool("neusoft");
        s1.showMessage();
         
        
        Student s2 = new Student();
        s2.setId("102");
        s2.setName("bbb");
        s2.setAge(22);
        s2.setSchool("neu");
        s2.showMessage();
        
        System.out.println(s1.getName()+"-"+s1.getSchool());
        System.out.println(s2.getName()+"-"+s2.getSchool());
    }
}

package com.foreknow.extend;

public class Test {
    public static void main(String[] args) {
        Person person = new Person();
        System.out.println(person.getName());
    }
}

你可能感兴趣的:(2019-07-31 Person与Student)