天天看點

類的多态性

【實驗十二】

1. 編寫一個Java應用程式,設計一個汽車類Vehicle,包含的屬性有車輪個數wheels和車重weight。小車類Car是Vehicle的子類,其中包含的屬性有載人數loader。卡車類Truck是Car類的子類,其中包含的屬性有載重量payload。每個類都有構造方法和輸出相關資料的方法。

class Vehicle {
	int wheels;//車輪個數
	double weight;//車重
	public int getwheels() {
		return wheels;
	}
	public void setwheels(int wheels) {
		this.wheels=wheels;
	}
	public double getweight() {
		return weight;
	}
	public void setweight(double weight) {
		this.weight=weight;
	}
	Vehicle(int wheels,double weight){
		this.wheels=wheels;
		this.weight=weight;
		
	}
}

class Car extends Vehicle{
	int loader;//載人數
	public int getloader() {
		return loader;
	}
	public void setloader(int lodaer) {
		this.loader=lodaer;
	}
	Car(int wheels,double weight,int loader){
		super(wheels,weight);
		this.loader=loader;	
		
	}
}
class Truck extends Car{
	double payloda;//載重
	public double getpayloda() {
		return payloda;
	}
	public void setpayloda(double payloda) {
		this.payloda=payloda;
	}
	Truck(int wheels,double weight,int loader,double payloda){
		super(wheels,weight,loader);
		this.payloda=payloda;
		
	}
}
public class TestVehicle {
	public static void main(String args[]) {
		Vehicle a = new Vehicle(6,4);
		System.out.println("車輪有"+a.getwheels()+"個、車重"+a.getweight()+"噸");
		Car b = new Car(1,2,3);
		System.out.println("車輪有"+b.getwheels()+"個、車重"+b.getweight()+"噸、能坐"+b.getloader()+"個人");
		Truck c = new Truck(4,5,6,7);
		System.out.println("車輪有"+c.getwheels()+"個、車重"+c.getweight()+"噸、能坐"
				+c.getloader()+"個人、載重"+c.getpayloda()+"噸");
	}
}
           

2.定義一個類,類中有計算體積的兩個同名方法,分别可計算圓柱體體積與長方體體積,舉例驗證程式。

class V {
	double h;//高
	double l;//長
	double s;//寬
	double r;//底面半徑
	public double geth() {
		return h;
	}
	public void seth(double h) {
		this.h=h;
	}
	public double getl() {
		return l;
	}
	public void setl(double l) {
		this.l=l;
	}
	public double gets() {
		return s;
	}
	public void sets(double s) {
		this.s=s;
	}
	public double getr() {
		return r;
	}
	public void setr(double r) {
		this.r=r;
	}
	V(double h,double l,double s){
		this.h=h;
		this.l=l;
		this.s=s;
		System.out.println("長方體的體積是"+h*l*s);
	}
	V(double r,double h){
		this.r=r;
		this.h=h;
		System.out.println("圓柱的體積是"+r*r*h*3.14);
	}
}
public class TestV {
	public static void main(String args[]) {
		V c = new V(1,2,3);
		V y = new V(3,4);
	}
}
           

3.編碼建立一個手機類Phones,定義打電話方法call()。建立兩個子類:蘋果手機類IPhone和安卓手機類APhome,并在各自類中重寫方法call(),編寫程式入口main()方法中實作兩種手機打電話。

public class Phones {
	String pinpai;//品牌
	double money;//錢
	public void setpinpai(String pinpai) {
		this.pinpai = pinpai;
	}
	public String getpinpai() {
		return pinpai;
	}
	public void setmonet(double money) {
		this.money = money;
	}
	public double getmoney() {
		return money;
	}
	Phones(String pinpai,double money){
		this.money = money;
		this.pinpai = pinpai;
	}
	public void call(){
		System.out.println("我正在使用"+pinpai+"牌的手機打電話,這款手機價值"+money+"元。");
	}
}
public class IPhone extends Phones{
	IPhone(String pinpai,double money){
		super(pinpai,money);
	}
	public void call() {
		System.out.println("我正在使用"+super.getpinpai()+"牌的手機打電話,"
				+ "這款手機價值"+super.getmoney()+"元。biu~biu~biu~");
	}
}
public class APhone extends Phones{
	APhone(String pinpai,double money){
		super(pinpai,money);
	}
	public void call() {
		System.out.println("我正在使用"+super.getpinpai()+"牌的手機打電話,"
				+ "這款手機價值"+super.getmoney()+"元。~~~~~");
	}
}
public class Main {
	public static void main(String[] args) {
		APhone a = new APhone("安卓",3000);
		a.call();
		IPhone i = new IPhone("蘋果",5000);
		i.call();
	}
}
           

4.閱讀下面程式的程式,分析輸出結果,并上機驗證。

(1)程式1:

class A{

     void callme( ) {

      System.out.println(“Inside A’s callme()method”);

     }   }   class B extends A{

     void callme( ) {

      System.out.println(“Inside B’s callme() Method”);

     }   }   public class Dispatch{

     public static void main(String args[]) {

      A a=new B();

      a.callme( );

     }   }

上面程式的輸出結果:

Inside B’s callme() Method

(2)程式2:

class AA{

double x=1.1;

double method(){

return x;

}

}

class BB extends AA{

double x=2.2;

double method(){

return x;

}

}

1) 類AA和類BB是什麼關系?

AA是父類,BB是子類

2) 類AA和類BB中都定義了變量x和method()方法,這種情況稱為什麼?

方法的重載

3) 若定義AA a=new BB(); 則a.x和a.method()的值是什麼?

1.1、2.2

(3)程式3:

class AA{

public AA(){

System.out.println(“AA”);

}

public AA(inti){

this();

System.out.println(“AAAA”);

}

public static void main(String args[]){

BB b=new BB();

}

}

class BB extends AA{

public BB(){

super();

System.out.println(“BB”);

}

public BB(inti){

super(i);

System.out.println(“BBBB”);

}

}

1) 上面程式的輸出結果是什麼?

AA

BB

2)若将main()方法中的語句改為:B b=new B(10); 程式輸出的結果是什麼?

AAAA

BBBB