• [零基础学JAVA]Java SE面向对象部分.面向对象基础(05)


    1.继承

    2.多态

    3.final

    4.重载与覆写

    5. this/super

    6.抽象类

    7.接口

    java:

    class Person{

        private String name;
        private int    age;

        public Person(){
            System.out.println("Person");
        }
        public Person(String arg){
            System.out.println(arg);
        }

        //this关键字的时候强调过一句话:如果一个类之中有多个构造方法之间使用this()互相调用的话,
        //那么至少要保留有一个构造方法作为出口,而这个出口就一定会去调用父类构造。
        //
        //         此时在某种程度上来讲,有一个问题解释了一半:一个简单Java类一定要保留有一个无参构造方法。
    //由于java不管理内存,无析构函数
    /*
        ~Person(){
            System.out.println("~Person");
        }
        */

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

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

        public int getAge(){
            return this.age;
        }

    //覆写方法,被覆写的方法不能拥有比子类更严格的权限
    //private < default < public

        public void  show(){
            System.out.println("Person show");
            System.out.println(this.getAge());
            System.out.println(this.getName());
        }

    };

    //不允许多重继承,一个子类只允许继承一父类。
    class Man extends Person{

        private int weight;
        private int hight;
        Man(){
            // 必须放有第一行
            // super调用父类构造的时候,一定要放在构造方法的首行上。
            super("super");
            System.out.println("Man");
        }
        Man(String arg){
            // 必须放有第一行
            // super调用父类构造的时候,一定要放在构造方法的首行上。
            //super("super");
            this();//调用本类构造
            System.out.println("Man");
        }
        public void setWeight(int weight){
            this.weight = weight;
        }

        public int getWeight(){
            return this.weight;
        }

        public void setHight(int hight){
            this.hight = hight;
        }
        public int getHight(){
            return this.hight;
        }
        public void show(){
            //如果要调用被覆写过的父类的方法super()
            //· this.方法():先从本类查找是否存在指定的方法,如果没有找到,则调用父类操作;
            //· super.方法():直接由子类调用父类之中的指定方法,不再找子类。
            //覆写发生在继承关系中,注意权限,参数个数,权限不能比父类严格,函数名称,返回值
            //重载发生在一个类中,无权限要求,参数个数或是类型不同,返回值不能确定是否是重载
            //super.show();
            System.out.println("Man show");
            System.out.println(this.getWeight());
            System.out.println(this.getHight());
        }

    };

    abstract class Woman{
        private boolean ispre;
        public int     longth;
        
        Woman(int longth){
            this.longth = longth;
        }
        public abstract boolean getIspre();

    }

    class ImpWoman extends Woman{
        ImpWoman(int a){
            super(a);
        }
        public boolean getIspre(){
            if(super.longth>15)
                return true;
            else
                return false;
        }
    }

    public class InheritTest{
        public static void main(String args[]){
            Man man = new Man();
            Person per= new Person();
            per.setAge(1000);
            man.setWeight(10000);
            per.show();
             //子类在调用时调用的都是覆写函数
            man.show();
            
            System.out.println("=======================================");
            //System.out.println(man.getAge());
            //C++可以进行类型强转,java 相同
            Person pper = new Man() ;//向上转型
            pper.show();
            Man  mman = null;
            mman = (Man)pper;

            if(pper instanceof Person)
                System.out.println("true");
            else
                System.out.println("false");      

            if(per instanceof Man)
                System.out.println("true");
            else
                System.out.println("false");

            //         · 向下转型,10%,因为在进行向下转型操作之前,一定要首先发生向上转型,以建立两个对象之间的联系,如果没有这种联系,是不可能发生向下转型的,一旦发生了运行中就会出现“ClassCastException”,当需要调用子类自己特殊定义方法的时候,才需要向下转型;
        //    Man mman = (Man)new Person();//向下转型
            /*
            Man mman = new Person();
            mman.show();
            */
            //抽象类不能被实例化
            //Woman woman = new Woman(23);

            //抽象类之中可以没有抽象方法,但是反过来讲,如果有抽象方法,则一定是抽象类,即使抽象类之中没有抽象方法,也不能够被直接实例化
            ImpWoman impwoman = new ImpWoman(23);
            if(impwoman.getIspre())
                System.out.println("true");
        }
    }
     C++:
    #include <iostream>
    #include <string>
    #include <stdlib.h>
    using namespace std;

    class Person{
        private:
            string name;
            int    age;

        public:
        Person(){
            cout<<"Person"<<endl;
        }
        ~Person(){
            cout<<"~Person"<<endl;
        }
        void setName(string name){
            this->name = name;
        }  
        string getName(){
                return this->name;
        } 
        void setAge(int age){
                this->age = age;
        }  
        int getAge(){
            return this->age;
        }
    };

    class Cloth{
        private:
            int color;
            int longth;
       public:
            void show();

    };

    //C++方法可以在类内部声明,外部定义,java则不可以
    void Cloth::show(){
        cout<<"test"<<endl;
    }

    //只能用于继承的类,不能用于实例化的类。
    //父类中无法确定具体实现的成员函数称为纯虚函数。纯虚函数是一种特殊的虚函数,
    //它只有声明,没有具体的定义。抽象类中至少存在一个纯虚函数;存在纯虚函数的类一定是抽象类。存在纯虚函数是成为抽象类的充要条件。
    //     virtual 返回值类型成员函数名(参数表)=0;
    //     拥有纯虚函数的类称之为抽象类
    class Animal{
        private:
            int type;
            int age;
            bool hair;
        public:
            Animal(int type,int age,bool hair){
                this->type = type;
                this->age  = age;
                this->hair = hair;
            }
            void setType(int type){
                this->type = type;
            }
            void setAge(int age){
                this->age = age;
            }

            int getType(){
                return this->type;
            }
            int getAge(){
                return this->age;
            }
            virtual void show() = 0;

    };

    class Dog:public Animal{
        private:
            int legs;
        public:
            Dog(int type,int age,bool hair,int legs):Animal(type,age,hair),legs(legs){}

            //纯虚函数可以被覆写,必须被覆写
            void show(){
                cout<<"legs :"<<this->legs<<endl;
            }
    };
           
    //c++允许多重继承,一个子类可以继承多个父类
    class Man:public Person,public Cloth{
        private:
            int weight;
            int hight;

        public:
        Man(){
            cout<<"Man"<<endl;
        }
        ~Man(){
            cout<<"~Man"<<endl;
        }
        void setWeight(int weight){
            this->weight = weight;
        }
        int getWeight(){
            return this->weight;
        }
        void setHight(int hight){
            this->hight = hight;
        }
        int getHight(){
            return this->hight;
        }
    };


    int main(int argc,char *argv[]){
            Man man;
            man.setAge(10);
            cout<<man.getAge()<<endl;
            //抽象类不能被实例化
            //Animal animal(0,0,true);
            Dog dog(1,12,true,4);
            dog.show();
    }

     Final:

    java:

    //final 声明的类不能有子类,声明的方法不能被子类覆写,声明的变量是常量,不能被修改 
    class Person{

        private String name;
        private int    age;
        final  int read = 5;

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

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

        public int getAge(){
            return this.age;
        }

        
    };


    public class FinalTest{

        public static void main(String args[]){
            Person per = new Person();
            //不能被修改
            //per.read = 10;
            System.out.println(per.read);

        }
    }
     C++:
    #include <iostream>
    #include <string>
    using namespace std;
    //final 声明的类不能有子类,声明的方法不能被子类覆写,声明的变量是常量,不能被修改 
    class Person{
        private :
            string name;
            int age;
        public:
        const int read ;
        Person(int a):read(a){}
        void setName(string name){
            this->name = name;
        }

        void setAge(int age){
            this->age = age;
        }

        int getAge(){
            return this->age;
        }
    };
    //const int Person::read = 5;
    int main(){
            Person per(10);
            //不能被修改
        //    per.read = 20;
            cout<<per.read<<endl;
            return 0;

    }
     
  • 相关阅读:
    gulp-API介绍
    前端构建工具gulpjs的使用介绍及技巧(转载)
    atom插件之less-autocompile
    atom-安装插件
    gulp入门1
    edp 基于node.js和npm的前端开发平台
    (转)详解JavaScript模块化开发
    require.js
    thinkcmf5 iis+php重写配置
    thinkcmf5 模板版变量的加载过程 和 新增网站配置项怎么全局使用
  • 原文地址:https://www.cnblogs.com/zhangsf/p/3337048.html
Copyright © 2020-2023  润新知