zoukankan      html  css  js  c++  java
  • 继承

    package day01;

    public class Employee {
    String name;
    int num;

    public Employee() {
    System.out.println("wuc fu Employee");
    }
    public Employee(String name,int num){
    System.out.println("youc fu Employee");
    this.name=name;
    this.num=num;
    }
    public String getName() {
    return name;
    }

    public int getNum() {
    return num;
    }

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

    public void setNum(int num) {
    this.num = num;
    }

    public void m(){
    System.out.println("我想你了"+this.name);
    }
    }


    package day01;


    public class Teacher extends Employee{//一个子类只能有一个父亲,父亲有多个儿子
    public Teacher() {
    System.out.println("wuc zi Teacher");
    }
    public Teacher(String name,int num){
    super(name,num);//super可以调用父类有参方法
    System.out.println("youc zi Teacher");
    /*this.name=name;
    this.num=num;*/
    }
    @Override //检测是否重写正确
    public void m(){
    System.out.println("我是老师");
    System.out.println("我想你了"+this.name);
    System.out.println("我想你了"+super.name);//super是指向父类
    }
    }

    package day01;

    public class It extends Employee{
    public It() {
    System.out.println("wuc zi IT");
    }
    public It(String name,int num){
    super(name,num);//super可以调用父类有参方法 这样就不用自动调用无参的了
    System.out.println("youc zi IT");
    /* this.name=name;
    this.num=num;*/
    }
    @Override //检测是否重写正确
    public void m(){
    System.out.println("我是程序员");
    System.out.println("我想你了"+this.name);
    System.out.println("我想你了"+super.name);//super指向父类
    }
    }

    package day01;

    public class Case13 {
    public static void main(String[] args) {
    It a=new It("Lucken",18); //子类 //先调用父类无参构造方法,再调用子类构造方法
    Teacher b=new Teacher("luying",22); //子类
    /* a.name="Lucken";
    b.name="luying";
    a.num=18;
    b.num=22;*/
    a.m(); //子类特有方法 可以用父类(先用子类,子类没有用父类)
    //(子类返回值范围要小于父类,子类的权限要大于等于父类)
    b.m(); //子类特有方法 可以用父类
    Employee c=new Employee(); //父类不能用子类方法
    c.name="jae";
    c.num=21;
    c.m();
    }

    }
  • 相关阅读:
    javascript闭包和作用域链
    关于git的简单实用命令
    springMVC配置Json
    Python基础教程(010)--第一个程序Hello Python
    Python基础教程(009)--Python程序的格式以及扩展名
    Python基础教程(008)--第一个Python程序
    Python基础教程(007)--Python的优缺点
    Python基础教程(006)--Python的特点
    Python基础教程(005)--为什么要学习Python?
    Python基础教程(004)--Python的设计哲学
  • 原文地址:https://www.cnblogs.com/worldof/p/10672031.html
Copyright © 2011-2022 走看看