Java面向对象多态性

一、多态性的体现java

                方法的重载和重写
ide

                对象的多态性
this

二、对象的多态性:code

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

                                父类  父类对象=子类实例
接口

                        向下转型:强制类型转换
get

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

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();
	}

}

多态性的应用程序

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();
		}
		
	}



 Java面向对象instanceof关键字 方法


一、在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);
	}

}

 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();
	}

}


接口的使用

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());
	}

}

相关文章
相关标签/搜索