Java面向对象多态性

1、多态性的体现

                方法的重载和重写

                对象的多态性

2、对象的多态性:

                    向上转型:程序会自动完成

                                父类  父类对象=子类实例

                        向下转型:强制类型转换

                                    子类   子类对象=(子类)父类实例

package com.jk.ref;

class A{
	public void tell1(){
		System.out.println("A---tell1");
	}
	public void tell2(){
		System.out.println("A---tell2");
	}
}
class B extends A{
	public void tell1(){
		System.out.println("B---tell1");
	}
	public void tell3(){
		System.out.println("B---tell3");
	}
}

public class PelDemo01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//向上转型
		B b=new B();
		A a=b;			
		a.tell1();		//这是重写的方法
		a.tell2();		//
		//向下转型  必须先发生向上转型
		A a2=new B();		//注意:右边的是B,实例化的是B
		B b2=(B)a2;
		b2.tell1();
		b2.tell2();
		b2.tell3();
	}

}

130902_sdaj_2356966.png

多态性的应用

package com.jk.ref;
class A1{
	public void tell1(){
		System.out.println("A1---tell1");
	}
}
class B1 extends A1{
	public void tell2(){
		System.out.println("B1---tell2");
	}
}
class C1 extends A1{
	public void tell3(){
		System.out.println("C1---tell3");
	}
}
class D1 extends A1{
	public void tell4(){
		System.out.println("C1---tell3");
	}
}
public class PpDemo01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
			say(new B1());
			say(new C1());
			say(new D1());
	}
		public static void say(A1 a){
			a.tell1();
		}
		
	}

133822_NcCt_2356966.png



 Java面向对象instanceof关键字 


1、在Java中可以使用instanceof关键字判断一个对象到底是不是一个类的实例

package com.jk.ref;

class A{
	public void tell1(){
		System.out.println("A---tell1");
	}
	public void tell2(){
		System.out.println("A---tell2");
	}
}
class B extends A{
	public void tell1(){
		System.out.println("B---tell1");
	}
	public void tell3(){
		System.out.println("B---tell3");
	}
}

public class PelDemo01 {

	public static void main(String[] args) {
		A a=new A();
		System.out.println(a instanceof A);
		System.out.println(a instanceof B);
		
		A a1=new B();
		System.out.println(a1 instanceof A);
		System.out.println(a1 instanceof B);
	}

}

134327_u2x3_2356966.png

 Java面向对象抽象类应用

package com.jk.ref;
abstract class Renlei{
	private int age;
	private String name;
	/**
	 * @return the age
	 */
	public Renlei(int age,String name){
		this.age=age;
		this.name=name;
	}
	public int getAge() {
		return age;
	}
	/**
	 * @param age the age to set
	 */
	public void setAge(int age) {
		this.age = age;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	public abstract void want();
	
}
class Student extends Renlei{
	private int score;
	
	/**
	 * @return the score
	 */
	public int getScore() {
		return score;
	}
	/**
	 * @param score the score to set
	 */
	public void setScore(int score) {
		this.score = score;
	}
	public Student(int age, String name,int score) {
		super(age, name);
		this.score=score;
		// TODO Auto-generated constructor stub
	}
	public void want(){
		System.out.println("name:"+getName()+"   age:"+getAge()+"  score"+getScore());
	}
	
}
class Worker extends Renlei{
	private int money;

	/**
	 * @return the money
	 */
	public int getMoney() {
		return money;
	}

	/**
	 * @param money the money to set
	 */
	public void setMoney(int money) {
		this.money = money;
	}
	public Worker(int age, String name,int money) {
		super(age, name);
		this.money=money;
		// TODO Auto-generated constructor stub
	}
	public void want(){
		System.out.println("name:"+getName()+"   age:"+getAge()+"  money"+getMoney());
	}
	
	
}
public class Ddemo01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Student s=new Student(19,"xiaoming",88);
		s.want();
		Worker w=new Worker(40,"daming",2000);
		w.want();
	}

}

140421_WbUH_2356966.png


接口的使用

package com.jk.ref;

interface USB{
	void start();
	void stop();
}
class C{
	public static void work(USB u){
		u.start();
		System.out.println("working");
		u.stop();
	}
}
class USBisk implements USB{

	@Override
	public void start() {
		// TODO Auto-generated method stub
		System.out.println("U盘开始工作");
	}

	@Override
	public void stop() {
		// TODO Auto-generated method stub
		System.out.println("U盘停止工作");
	}
	
}
class Printer implements USB{

	@Override
	public void start() {
		// TODO Auto-generated method stub
		System.out.println("打印机开始工作");
	}

	@Override
	public void stop() {
		// TODO Auto-generated method stub
		System.out.println("打印机停止工作");
	}
	
}
public class InteDemo01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		C.work(new USBisk());
		C.work(new Printer());
	}

}

143407_F3zK_2356966.png

转载于:https://my.oschina.net/u/2356966/blog/409074

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值