阅读 124

一起Talk Android吧(第十三回:Java面向对象综合练习)

各位看官们,大家好,上一回中咱们说的是Java中多态的例子,这一回咱们说的例子是Java面向对象综合练习。闲话休提, 言归正转。让我们一起Talk Android吧!

看官们,我们在前面的章回中介绍了面向对象的内容,而且大部分是概念或者说理论上的内容,代码也都是伪代码,估计许多看官都想自己动手试试了。我们在本章回中就和大家一起动手试试面向对象的内容。

public class oop {
    public static void main(String args[])
    {
        Father father = new Father(60,"Zhang DaGou");
        Father.familyName = "Zhang";
        Son son = new Son(30,"Zhang XiaoGou");

        System.out.println("Father and Son have the same family name:"+Father.familyName);
        System.out.println("Father is:"+father.getName()+", Father age is:"+father.getAge());
        father.mySon();
        father.myDaughter();
        System.out.println("Son is:"+son.getName()+", Son age is:"+son.getAge());
        son.mySon();
        son.myDaughter(); // call the func of Class Father
        son.myDaughter("Zhang MaoMao");
        son.printWork();
        System.out.println();

        System.out.println("----------- Before changing the type of object -----------");
        //son = father; // this is wrong
        System.out.println("father instanceof Father is:"+(father instanceof Father));
        System.out.println("father instanceof Son is:"+(father instanceof Son));        
        System.out.println("son instanceof Father is:"+(son instanceof Father));
        System.out.println("Son instanceof Son is:"+(son instanceof Son));

        father = son;   
        System.out.println("----------- After changing the type of object -----------");

        System.out.println("father instanceof Father is:"+(father instanceof Father));
        System.out.println("father instanceof Son is:"+(father instanceof Son));        
        System.out.println("son instanceof Father is:"+(son instanceof Father));
        System.out.println("Son instanceof Son is:"+(son instanceof Son));

        System.out.println();   
        father.mySon(); // call the func of Class Son
        father.myDaughter(); // call the func of Class Father
        //father.myDaughter("Zhang MaoMao"); //this is wrong, it can't call the func of Class Son       

    }
}

class Father {
    public int age;
    public String name;
    public static String familyName;

    public Father(int a, String n){
        age = a;
        name = n;
    }

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

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

    public void mySon(){ 
        System.out.println("My son is Zhang XiaoGou");
    }

    public void myDaughter(){
        System.out.println("I have not daughter" );
    }
}

class Son extends Father implements Work{
    public Son(int a, String n){
        super(60,"MyFather");
        this.age = a;
        this.name = n;      
    }

    // Overriding this function
    public void mySon(){ 
        System.out.println("My son is Zhang XiaoXiaoGou");
    }

    // Overloading this function
    public void myDaughter(String name){  
        System.out.println("My daghter is not born,but we make a name for her: "+name);
    }

    public void printWork()
    {
        System.out.println("I am working at Zoo");
    }
}

interface Work{
    public void printWork();
}
复制代码

从上面的代码中可以看到,我们定义了两个类:Father和Son;一个接口:Work。它们三个封装了相应的成员变量和成员函数,大家可以从代码中看到,我们就不一一列出。在定义类Son时,它继承了类Father并且实现了Work接口。Son实现了Work接口中的抽象函数,当然了功能比较简单,只有一个打印语句,不过,大家需要从中体会到如何实现接口。Son继承了Father的全部成员变量和成员函数,在继承的同时还重写了mySon()函数。除此之外它还重载了myDaughter()函数。大家需要从中看到重写和重载的区别。接着我们判断了对象father和son的类型,我们把转换前和转换后的类型都打印出来了,大家可以在程序运行结果中看到。 判断完类型后就可以转换对象的类型了,这便是多态的一种体现。

多态的另外一种体现就是调用子类的函数,代码中调用的是被重写的函数mySon()。下面是程序的运行结果,请大家参考:

Father and Son have the same family name:Zhang
Father is:Zhang DaGou, Father age is:60
My son is Zhang XiaoGou
I have not daughter
Son is:Zhang XiaoGou, Son age is:30
My son is Zhang XiaoXiaoGou
I have not daughter
My daghter is not born,but we make a name for her: Zhang MaoMao
I am working at Zoo

----------- Before changing the type of object -----------
father instanceof Father is:true
father instanceof Son is:false
son instanceof Father is:true
Son instanceof Son is:true
----------- After changing the type of object -----------
father instanceof Father is:true
father instanceof Son is:true
son instanceof Father is:true
Son instanceof Son is:true

My son is Zhang XiaoXiaoGou
I have not daughter
复制代码

看官们,以上代码属于比较基础的代码,大家可以结合代码中的注释,以及代码的运行结果来分析代码的含义。希望大家能够从中体会到面向对象中:封装,继承和多态的含义。此外,代码中的名字、年龄等都是虚拟构造的,猫猫狗狗的一家子很热闹呀,大家看着开心就好。

各位看官,关于Java面向对象综合练习的例子咱们就介绍到这里,欲知后面还有什么例子,且听下回分解!

文章分类
Android
文章标签