达内课程-面向对象之多态

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u010356768/article/details/75733094

多态

1、作用:一致的类型
多种子类型都可以转换成一致的父类型
2、类型转换
1)向上转型
子类对象转为父类型
//向上转型后,只能调用父类定义的通用成员
//不能调用子类特有成员
//可以先向下转回成子类型再调用
2)向下转型
已经转为父类型的子类对象再转回子类型

Human类举例

其他代码可以看达内课程8-面向对象(三)

这里只改了MainActivity代码

public class MainActivity extends AppCompatActivity {

    Button button1;
    Button button2;
    Button button3;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);
        textView = (TextView) findViewById(R.id.textView);
    }

    public void doClick(View view) {
        switch (view.getId()) {
            case R.id.button1:
                f1();
                break;
            case R.id.button2:
                f2();
                break;
            case R.id.button3:
                f3();
                break;
        }
    }

    private void f1() {
        Human human = new Human("kk","女",18);
        textView.setText(human.tostring());

        f(human);
    }
    private void f2() {
        Student  student = new Student();
        student.name ="小A";
        student.gender ="男";
        student.age =10;
        student.school = "北大";
        textView.setText(student.tostring());
        //学生类型向上转型成Human类型,传递到f()方法
        f(student);
    }
    private void f3() {
        Employee  employee = new Employee();
        employee.name ="小B";
        employee.gender ="女";
        employee.age =22;
        employee.company = "冀春";
        textView.setText(employee.tostring());

        f(employee);
    }

    /*
    * 处理父类型对象
    *所有子类型对象都可以转成父类型,传递到这个方法进行处理
    * 如果传递的是父类型,直接调用父类型的tostring();方法
    * 如果传递的是子类型,则调用子类型的tostring();方法
    * */
    void f(Human human){
        textView.append("\n"+human.tostring());
    }
}

如果运行f(2);时,加断点,可以看到传到f(Human human);方法中的是Student类型
Human断点

电子宠物修改

其余项目没有变达内课程8-面向对象(三)

MainActivity

public class MainActivity extends AppCompatActivity {
    Dog dog;
    Cat cat;

    Button create;
    Button play;
    Button feed;
    Button punish;

    Button create1;
    Button play1;
    Button feed1;
    Button punish1;

    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        create = (Button)findViewById(R.id.button1);
        play = (Button)findViewById(R.id.button2);
        feed = (Button)findViewById(R.id.button3);
        punish = (Button)findViewById(R.id.button4);
        create1 = (Button)findViewById(R.id.button5);
        play1 = (Button)findViewById(R.id.button6);
        feed1 = (Button)findViewById(R.id.button7);
        punish1 = (Button)findViewById(R.id.button8);

        textView = (TextView)findViewById(R.id.textView);
    }

    public void doClick(View view) {
        switch (view.getId()) {
            case R.id.button1:
                f1();
                break;
            case R.id.button2:
                f2(dog);
                break;
            case R.id.button3:
                f3(dog);
                break;
            case R.id.button4:
                f4(dog);
                break;
            case R.id.button5:
                f5();
                break;
            case R.id.button6:
                f2(cat);
                break;
            case R.id.button7:
                f3(cat);
                break;
            case R.id.button8:
                f4(cat);
                break;
        }
    }

    private void f5() {
        cat = new Cat();
        cat.name = "小咪";
        cat.hungry = 50;
        cat.happy = 50;

        textView.setText("电子猫已经创建");
    }

    private void f1(){
        dog = new Dog();
        dog.name = "蠢狗";
        dog.hungry = 50;
        dog.happy = 50;

        textView.setText("电子狗已经创建");
    }

    private void f2(Pet pet){
        pet.play(textView);
    }
    private void f3(Pet pet){
        pet.feed(textView);
    }
    private void f4(Pet pet){
        pet.punish(textView);
    }
}

instanceof
运行期类型识别
对真实类型,父类型判断都返回true

Point point = new Point3D();//向上转型
point instanceof Point3D;//true
point instanceof Point;//true

Point类转型练习

Point3D

 @Override
    public double distance(Point p) {
        int dx = x - p.x;
        int dy = y - p.y;
        int dz;

        //当前点是三维点
        //与参数点p求距离,p也可以是二维点也可以是三维点
        //需要判断p是二维点还是三维点
        if(p instanceof Point3D){
            //向上转型后,只能调用父类定义的通用成员
            //不能调用子类特有成员
            //可以先向下转回成子类型再调用
            dz = z - ((Point3D) p).z;
        }else{
            dz = z;
        }
        return Math.sqrt(dx*dx+dy*dy+dz*dz);
    }

练习:绘制图形
Shape
|-Line
|-Circle
|-Square

Share

public class Shape {
    public void draw(TextView view){
        //无意义代码,会在子类中重写
        view.setText("图形");
    }
    public void clean(TextView view){
        view.setText("");
    }
}

Line

public class Line extends Shape {
    @Override
    public void draw(TextView view) {
        super.draw(view);
        view.setText("-");
    }

    public void length(TextView view){
        view.append("\n很长很长...");
    }
}

Circle

public class Circle extends Shape {
    @Override
    public void draw(TextView view) {
        super.draw(view);
        view.setText("o");
    }
}

Square

public class Square extends Shape {
    @Override
    public void draw(TextView view) {
        super.draw(view);
        view.setText("口");
    }
}

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              >


    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Shape"
        android:onClick="doClick"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Line"
        android:onClick="doClick"/>
    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Circle"
        android:onClick="doClick"/>
    <Button
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Squre"
        android:onClick="doClick"/>

    <Button
        android:id="@+id/button5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="擦除"
        android:onClick="doClick"/>

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

MainActivity

public class MainActivity extends AppCompatActivity {
    Button button1;
    Button button2;
    Button button3;
    Button button4;
    Button button5;
    TextView textView;
    private Shape currentShape;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView)findViewById(R.id.text);
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);
        button4 = (Button) findViewById(R.id.button4);
        button5 = (Button) findViewById(R.id.button5);

    }

    public void doClick(View view) {
        switch(view.getId()){
            case R.id.button1:
                f(new Shape());
                break;
            case R.id.button2:
                f(new Line());
                break;
            case R.id.button3:
                f(new Circle());
                break;
            case R.id.button4:
                f(new Square());
                break;
            case R.id.button5:
                f1();
                break;
        }
    }


    private void f(Shape shape) {
        //参数对象,保存到成员变量
        currentShape = shape;
        shape.draw(textView);
        //向上转型后只能访问父类定义的通用成员
        //不能访问子类特有成员
        //shape.length();
        if(shape instanceof Line){
            Line line = (Line)shape;
            line.length(textView);
        }
    }

    private void f1(){
        if(currentShape != null){
            currentShape.clean(textView);
            currentShape = null;
        }
    }
}
展开阅读全文

没有更多推荐了,返回首页