Java 类的继承

package com.jasonote.classStudy;

import com.sun.deploy.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jasonote on 2018/1/13.
 * <p>
 * a,类的继承的优点
 * 子类可以直接将父类的操作继续使用,属于代码重用
 * 子类可以继续扩充属于自己的标准
 * <p>
 * b,类的限制
 * 不允许多重继承
 * <p>
 * c,覆写原则
 * 发现父类中的方法功能不足本类的要求,但必须使用这个方法时
 * 被覆写的方法不能够拥有比父类更为严格的访问权限(public, default, private)
 * 父类的方法被子类覆写,子类的权限只能比父类的方法大
 * <p>
 * 重载与覆写的区别(Overloading 与 Override)
 * <p>
 * 覆写发生范围:一个类里;继承中
 * 定义:方法名称参数类型及个数相同;方法名称,参数类型,个数,返回类型相同
 * 权限:没有权限限制; 被覆写的方法不能拥有比父类更为严格的访问权限
 * <p>
 * 在使用重载(Overloading)的时候返回值可以不同,但是考虑到程序设计的统一性,尽量保证一致
 */
public class ExtendsClass {
    public static void main(String[] args) {
        System.out.println("=================Teach====================");
        List<String> classes = new ArrayList<>();
        classes.add("大一101班");
        classes.add("大一201班");
        classes.add("大二301班");
        Teach teach = new Teach("Jason", 26, classes, "JAVA语言");
        System.out.println(teach);

        System.out.println("================Teach show demo=====================");
        teach.demo(teach.getName(), teach.getAge());

        System.out.println("================Stu=====================");
        Stu stu = new Stu("Jasonote", 18, "计算机编程", 107, teach);
        System.out.println(stu);

        System.out.println("================Stu show demo=====================");
        stu.demo(stu.getName(), stu.getAge(), false);

        System.out.println("================final=====================");
        FinalClass finalClass = new FinalClass();
        finalClass.FinalMethod();

        System.out.println(finalClass.MSG);
    }
}

class Person {

    private String name;

    private Integer age;

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    void demo(String name, Integer age) {
        System.out.println("name:" + name + ";age:" + age);
    }

    void demo(String name, Integer age, Boolean isShowAge) {
        if (isShowAge) {
            System.out.println("name:" + name + ";age:" + age);
        } else {
            System.out.println("name:" + name + ";不告诉你我的年龄,哈哈哈哈");
        }
    }

}

class Stu extends Person {
    private String StuClass;

    private Integer sno;

    private Teach teach;

    public Stu(String name, Integer age, String stuClass, Integer sno, Teach teach) {
        super(name, age);
        this.StuClass = stuClass;
        this.sno = sno;
        this.teach = teach;
    }

    public String getStuClass() {
        return StuClass;
    }

    public void setStuClass(String stuClass) {
        StuClass = stuClass;
    }

    public Integer getSno() {
        return sno;
    }

    public void setSno(Integer sno) {
        this.sno = sno;
    }

    public Teach getTeach() {
        return teach;
    }

    public void setTeach(Teach teach) {
        this.teach = teach;
    }

    /**
     * 覆写demo
     *
     * @param name
     * @param age
     */
    @Override
    public void demo(String name, Integer age) {
        System.out.println("名字:" + name + ";年龄:" + (age - 1));
    }

    @Override
    public String toString() {
        return "我的名字是:" + this.getName() + ";\n今年:" + this.getAge() + ";\n我的班级:" + this.getStuClass() + ";\n我的学号:" + this.getSno() + ";\n我的" + teach.getCourse() + "的老师:" + teach.getName();
    }
}

class Teach extends Person {
    private List<String> classes;

    private String course;

    public Teach(String name, Integer age, List<String> classes, String course) {
        super(name, age);
        this.classes = classes;
        this.course = course;
    }

    public List<String> getClasses() {
        return classes;
    }

    public void setClasses(List<String> classes) {
        this.classes = classes;
    }

    public String getCourse() {
        return course;
    }

    public void setCourse(String course) {
        this.course = course;
    }

    /**
     * 覆写demo
     *
     * @param name
     * @param age
     */
    @Override
    public void demo(String name, Integer age) {
        super.demo(name, age);
        System.out.println("----我再补充一些些----");
    }

    @Override
    public String toString() {
        return "我的名字是:" + this.getName() + ";\n今年:" + this.getAge() + ";\n我教的班级有:" + StringUtils.join(this.getClasses(), "; ") + ";\n我教的课程:" + this.getCourse();
    }
}

/**
 * 不可继承的类
 */
final class FinalClass {
    public static final String MSG = "我是全局常量";

    public void FinalMethod() {
        System.out.println("别想继承我,哈哈哈哈哈哈");
    }
}

/**
 * final FinalClass 不可继承
 */
//class FinalExtends extends FinalClass {
//
//}


com.intellij.rt.execution.application.AppMain com.jasonote.classStudy.Packagingclass
10
30
a == b: true
a == c: false
a == c: false
a.equals(c): true
i1==i2: false
i1.equals(i2): true
i3==i4: true
i3.equals(i4): true
123.0
false
10.07
10.07

Process finished with exit code 0

Jason.wang

When you find your talent can't afford to be ambitious, be quiet and study !

You may also like...