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

}

相關文章
相關標籤/搜索