JAVA面向对象程序设计-FeiGe快递系统-多态


JAVA面向对象程序设计-FeiGe快递系统-多态


package com.caishui.model;

public class TestSon {
public static void main(String[] args) {
	
	Son son=new Son();
	son.say();
	Parent parent=new Son();//向上转型
	parent.say();
	
	Son son1=(Son) parent;//向下转型
	son1.say();
	Parent parent2=new Parent();
	
	if (parent2 instanceof Son) {
		System.out.println("parent类型是son类型");
	} else {
		System.out.println("parent类型不是son类型");
		Son son2= (Son) parent;

	}
	
}




}

在这里插入图片描述

package com.caishui.model;



public class Parent {
	private String name;
	

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	public void say(){
		System.out.println("普通话");
	}

	public Parent() {
		super();
		// TODO Auto-generated constructor stub
		System.out.println("构造器");
	}
	
}

在这里插入图片描述

package com.caishui.model;

public class Son extends Parent {
  public Son (){
	  super();
	  System.out.println("========Son构造器=====");
  }
  public void say(){
	  System.out.println("普通话");
  }
}

在这里插入图片描述

发布了72 篇原创文章 · 获赞 10 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/BOGEWING/article/details/102543792
今日推荐