• 继承


    /*
    目前存在的问题:
        1. 无法描述清楚这两个类之间的继承关系。
        2. 存在着重复代码。
    
    面向对象的三大特征:
        1. 封装
        2. 继承
        3. 多态.
    
    继承:继承是通过关键字extends体现的。
    
    继承的格式:
    
        class 类名1 extends 类名2{
        
        }
    
    
    继承要注意的事项:
        1. 千万不要为了减少重复代码而去继承,只有真正存在着继承关系的时候才去继承。
        2. 父类私有的成员不能被继承。
        3. 父类的构造函数不能被继承。
        4. 创建子类对象时默认会先调用父类无参的构造函数。

        疑问: 为什么要调用父类的构造方法啊?这样子做的意义在那?

        调用父类的构造方法是可以初始化从父类继承下去的属性的。

    
    

        

    */
    //人类 
    class Person{
        
        String name;
    
        private    int age;
    
        public  Person(String name){
            this.name = name;
        }
    
        public Person(){
            System.out.println("Person类的构造方法被调用了....");
        }
    
        public void eat(){
            System.out.println(name+"在吃饭...");
        }
    }
    
    //学生类
    class Student extends Person {  // Student 就称作为Person类的子类, Person类就称作为Student的父类(超类、基类)
    
        int num; //学号
    
        public Student(){
            System.out.println("Student类的构造方法被调用了....");
        }
    
        public void study(){
            System.out.println(name+"good good study , day day up");
        }    
    }
    
    
    
    
    class Demo7 
    {
        public static void main(String[] args) 
        {
            Student s = new Student();
            
            /*
            s.name = "狗娃";
            System.out.println("名字:"+ s.name);
            s.eat();
            */
        }
    }
  • 相关阅读:
    多线程GCD(二)
    多线程
    Runtime & Runloop
    MTK android 重启测试脚本
    ubuntu samba 配置简介
    Gerrit使用简介
    MTK andorid从底层到上层添加驱动
    MTK GPIO 新增变量配置
    MT6755 使用R63350 IC 出现唤醒概率性闪白,并导致ESD FAIL
    android L版本AAL新架构
  • 原文地址:https://www.cnblogs.com/xufengyuan/p/6262876.html
Copyright © 2020-2023  润新知