• Java 入门25 继承 this super


     

     

     Person类

    package com.ITheima.d7_extends;
    
    public 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;
        }
    }
    View Code

    Student类继承Person类

    package com.ITheima.d7_extends;
    
    public class Student extends Person{
        public  void study(){
            System.out.println(getName()+"好好学习");
        }
    }
    View Code

    Teacher类继承Person类

    package com.ITheima.d7_extends;
    
    public class Teacher extends Person {
        public void teach() {
            System.out.println(getName()+"开始教导学生");
        }
    }
    View Code

    案例

     Person1类

    package com.ITheima.d8_extends;
    
    public class Person1 {
        private String name;
        private int age;
    
        public void watch(){
            System.out.println("查看课表");
        }
    
        //无参构造器
        public Person1() {
        }
    
        //有参构造器
        public Person1(String name, int age){
            this.name=name;
            this.age=age;
        }
    
        //set get
    
        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;
        }
    }
    View Code

    Student1类

    package com.ITheima.d8_extends;
    
    public class Student1 extends Person1 {
        private String stayClass;
        public void write(){
            System.out.println("填写听课反馈");
        }
    
        public String getStayClass() {
            return stayClass;
        }
    
        public void setStayClass(String stayClass) {
            this.stayClass = stayClass;
        }
    }
    View Code

    Teacher1类

    package com.ITheima.d8_extends;
    
    public class Teacher1 extends Person1 {
        private String place;
    
        public void sendMsg(){
            System.out.println("发布问题");
        }
    
        public String getPlace() {
            return place;
        }
    
        public void setPlace(String place) {
            this.place = place;
        }
    }
    View Code

    TestDemo11类

    package com.ITheima.d8_extends;
    
    import com.ITheima.d7_extends.Teacher;
    
    public class TestDemo11 {
        public static void main(String[] args) {
            Student1 s=new Student1();
            s.setName("TOM");//继承父类
            s.setAge(11);//继承父类
            s.setStayClass("梅花");
            s.write();
    
            Teacher1 t=new Teacher1();
            t.setName("Lili");//继承父类
            t.setAge(49);//继承父类
            t.setPlace("嘎嘎");
            t.sendMsg();
        }
    }
    View Code

     

     

     

     

     

     

     方法重写

     @Override重写注释

     Phone父类

    package com.ITheima.d8_extends;
    
    public class Phone {
        public void call(){
            System.out.println("开始打电话");
        }
    
        public void sendMessage(){
            System.out.println("发送短信开始");
        }
    }
    View Code

    NewPhone 子类

    package com.ITheima.d8_extends;
    
    public class NewPhone extends Phone{
        /**
         * 方法重写
         * @Override  放在重写后的方法上,作为重写是否正确的校验注解
         * 加上该注解后 如果重写错误,编译阶段会出现错误提示
         * (建议重写方法都加@Override注解,代码安全 ,优雅!)
         * 方法重写注意事项和要求
         * 1 重写方法的名称,形参列表与被重写的方法是的名称和参数列表一致
         * 2 私有方法不能被重写
         * 3 这类重写父类方法时 访问权限必须大于或等于父类(暂时了解:缺省<protected<public)
         * 4 子类不能重写父类的静态方法,如果重写会报错
         */
        @Override
        public void call(){
            super.call();//调用父类的call方法
            System.out.println("支持饰视频通过~~~~");
        }
    
        /**
         * 方法重写
         */
        @Override
        public void sendMessage(){
            super.sendMessage();//调用父类的sendMessage方法
            System.out.println("支持图片发送");
        }
    }
    View Code

    Test测试类

    package com.ITheima.d8_extends;
    
    public class Test {
        public static void main(String[] args) {
            NewPhone np=new NewPhone();
            np.call();
            np.sendMessage();
        }
    }
    View Code

     Animal类

    package com.ITheima.d9_extends;
    
    public class Animal {
        public Animal(){
            System.out.println("===父类Animal无参构造器被执行===");
        }
    }
    View Code

    Cat类

    package com.ITheima.d9_extends;
    
    public class Cat extends Animal{
        public Cat(){
            super();//默认的 ,写不写 都有,默认就是执行父类无参数构造器
            System.out.println("===========子类Cat 无参构造执行=========");
        }
    
        public Cat(String name){
            System.out.println(name+"======子类有参构造器执行=======");
        }
    }
    View Code

    Test类

    package com.ITheima.d9_extends;
    
    public class Test {
        public static void main(String[] args) {
            Cat c=new Cat();
            System.out.println("---------------------");
            Cat c1=new Cat("波斯猫");
        }
    }
    View Code

     Animal类

    package com.ITheima.d9_extends;
    
    public class Animal {
        private String name;
        private int age;
    
        //无参构造器
        public Animal() {
        }
    
        //有参构造器
        public Animal(String name,int age){
            this.name=name;
            this.age=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;
        }
    }
    View Code

    Cat类

    package com.ITheima.d9_extends;
    
    public class Cat extends Animal{
        //子类的私有成员变量
        private  String className;
    
        //无参构造器
        public Cat() {
        }
    
        //有参构造器
        public Cat(String name,int age,String className){
            //一定要访问父类的无参构造器(如果父类没有无参构造器 则需要手动访问父类的有参构造器 执行super(E element))
            super(name,age);//默认的 ,写不写 都有,默认就是执行父类无参数构造器
            this.className=className;
        }
    
        public String getClassName() {
            return className;
        }
    
        public void setClassName(String className) {
            this.className = className;
        }
    }
    View Code

    Test类

    package com.ITheima.d9_extends;
    
    public class Test {
        public static void main(String[] args) {
            Cat c=new Cat("波斯猫",5,"幼儿园");
            System.out.println("---------------------");
    
            System.out.println(c.getAge());
            System.out.println(c.getName());
            System.out.println(c.getClassName());
        }
    }
    View Code

     this  super

  • 相关阅读:
    比较实用的免费图标字库(转)
    聊聊 cursor鼠标样式
    (转)3款优秀的移动webAPP网站在线测试工具
    常用meta整理
    (转)SVN搭建(附下载地址)
    (转)C#串口SerialPort常用属性方法
    加载信息,先从数据库取出5条实现分页,鼠标向上滑动触发Ajax再加载5条,达到异步刷新,优化加载。。。
    图片上传,获取路径以及下载功能
    Mysql事务,并发问题,锁机制
    app中Webview实现下载表格
  • 原文地址:https://www.cnblogs.com/lvlisn/p/16410591.html
Copyright © 2020-2023  润新知