JAVA OOP 第三章多态3

本文详细探讨了JAVA面向对象编程中的多态性概念,解释了多态的含义,包括静态多态和动态多态。通过实例分析,展示了如何在JAVA中实现多态,并讨论了其在代码复用和设计灵活性方面的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package cn.happy4;

public class Main {

	public static void main(String[] args) {
	Singleton s1=Singleton.getInstance();
	Singleton s2=Singleton.getInstance();
	Singleton s3=Singleton.getInstance();
    System.out.println(s1==s2);
    System.out.println(s2==s3);
	
	}

}

package cn.happy4;

public class Singleton {
 private Singleton(){
	
}
private static Singleton single=new Singleton();
public static Singleton getInstance(){
	if(single==null){
		single=new Singleton();
		return single;
		
	}
	else{
		
		return single;
	}
	
}
}

==============================================================================================================================================
package cn.happy5;

public class DotMatrixPrinter extends Printer {

	@Override
	public void print() {
		// TODO Auto-generated method stub
System.out.println("针式打印机");
	}

}

package cn.happy5;

public class InkpetPrinter extends Printer {

	@Override
	public void print() {
		// TODO Auto-generated method stub
System.out.println("喷墨打印机");
	}

}

package cn.happy5;

public class LaserPrinter extends Printer {

	@Override
	public void print() {
		// TODO Auto-generated method stub
System.out.println("激光打印机");
	}

}

package cn.happy5;

public abstract class Printer {

	public abstract void print();
}

package cn.happy5;

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
      Printer p1=new DotMatrixPrinter();
      p1.print();
      Printer p2=new InkpetPrinter();
      p2.print();
      Printer p3=new LaserPrinter();
      p3.print();
	}

}


==================================================================================================================================

package cn.happy6;

public class Master {

	public void Doding(Person be){
		
		be.eat();
		be.play();
	}
	public Person getPerson(int id){
		Person per=null;
		if(id==1){
			per=new PersonA();
			
		}else if(id==2){
			
			per=new PersonB();
			
		}
		return per;
		
	}
	
	public void Play(Person be){
		if(be instanceof PersonA){
			PersonA pera=(PersonA)be;
			pera.play();
			
		}else if(be instanceof PersonB){
			
			PersonB perb=(PersonB)be;
			perb.play();
		}
		
	}
}

package cn.happy6;

public abstract class Person {

	
	public abstract void eat();
	public abstract void play();
	
}

package cn.happy6;

public class PersonA extends Person {


	public void eat() {
System.out.println("小强喜欢吃东北菜");
	}

	
	public void play() {
System.out.println("小强喜欢打雪仗");
	}

}

package cn.happy6;

public class PersonB extends Person{

	
	public void eat() {
		
System.out.println("约翰喜欢吃披萨");
	}


	public void play() {
		
System.out.println("约翰喜欢打橄榄球");
	}

}

package cn.happy6;
import java.util.Scanner;
public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("今天谁来(1.小强 2.约翰)");
int name=input.nextInt();
Master master=new Master();
Person per=master.getPerson(name);
per.eat();
per.play();
	}

}

================================================================================================================================================
package cn.happy7;

public class Bus extends MotoVehicle {

	public int seatCount;
	public int totalRent;
	public Bus(){}
	public Bus(String no,String brand,int seatCount){
		
		super(no,brand);
		this.seatCount=seatCount;
	}
	@Override
	public int calRent(int days) {
		// TODO Auto-generated method stub
		return totalRent=50*days;
	}

}

package cn.happy7;

public class Car extends MotoVehicle {

	public String type;
	public int totalRen;
	public Car(){
		
	}
	public Car(String no,String brand,String type){
		super(no,brand);
		this.type=type;
	}
	@Override
	public int calRent(int days) {
		// TODO Auto-generated method stub
		return totalRen=60*days;
	}

}

package cn.happy7;

public class Custerner {

	public String name="苏琳琳";
	public int calcTotalRent(MotoVehicle motos[],int days){
		
		int sum=0;
		for(int i=0;i<motos.length;i++){
			
			sum+=motos[i].calRent(days);
		}
		return sum;
	}
}

package cn.happy7;

public abstract class MotoVehicle {
public String no;
public String brand;
public int rentprice;
public MotoVehicle(){
	
	
}

public MotoVehicle(String no,String brand){
	this.no=no;
	this.brand=brand;
	
	
}
public abstract int calRent(int days);
}

package cn.happy7;

import java.util.Scanner;


public class TestRent {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
	Scanner input=new Scanner(System.in);
		int days;
		int totalRent=0;
		MotoVehicle[] motos=new MotoVehicle[5];
		motos[0]=new Car("京NG3543","宝马","商务舱GL8");
		motos[1]=new Car("京NG3534","宝马","商务舱GL8");
		motos[2]=new Car("京NG3565","别克","林荫大道");
		motos[3]=new Bus("京NG3354","解放",23);
		motos[4]=new Truck("京NG3221","卡车",10);
		days=5;
		Custerner cus=new Custerner();
		System.out.println("汽车牌号\t     汽车品牌");
		for(int i=0;i<motos.length;i++){
			totalRent+=cus.calcTotalRent(motos,days);
			System.out.println(motos[i].no+"\t  "+motos[i].brand);
		}
		System.out.println("客户名:"+cus.name+"租赁天数:"+days+"租赁总费用:"+totalRent+"元");
		Car car=new Car();
		car.no="京999999";
		car.brand="宝马";
		car.type="商务舱";
		car.totalRen=120;
		Bus bus=new Bus();
		bus.no="京AU8769";
		bus.totalRent=180;
		bus.brand="金杯";
		bus.seatCount=30;
		
		System.out.println("欢迎来到汽车租赁公司!!!!");
		System.out.println();
		System.out.println("请输入你要租赁的天数:");
		days=input.nextInt();
		
		System.out.println("请输入要租赁 的汽车型号(1.轿车  2.客车)");
		switch(input.nextInt()){
		case 1:
			System.out.println("请输入要租赁的轿车品牌(1.宝马 2.别克)");
			String brand=input.next();
			/*if(i==1){
				System.out.println("分配给你的汽车牌号是"+car.no);
				car.calRent(days);
				System.out.println("\n顾客你好!您需要支付的租赁费用是"+car.rentPrice);
		    car.calRent(days);
		    }
			if(i==2)*/
				System.out.println("你输入轿车的型号:2.商务舱GL8  3.林荫大道");
				String type=input.next();
				System.out.println("分配给你的汽车牌号是"+car.no);
				car.calRent(days);
				System.out.println("\n顾客你好!您需要支付的租赁费用是"+car.totalRen);
			
		
			break;
		case 2:
			System.out.println("您要租赁的客车品牌(1.金杯 2.金龙)");
			String seatCount=input.next();
			System.out.println("请输入客车的座位数:"+bus.seatCount);		
			System.out.println("分配给您的汽车牌号是"+bus.no);
			bus.calRent(days);		
			System.out.println("\n顾客您好!您需要支付的租赁费用是"+bus.totalRent);
			break;
		
		
		}
	
	}

}

package cn.happy7;

public class Truck extends MotoVehicle {
	
	public int tonnage;
	public Truck(){
		
		
	}
	
	
	public Truck(String no,String brand,int tonnage){
		super(no,brand);
		this.tonnage=tonnage;
		
	}
	public Truck(String no,String brand){
		super(no,brand);
		
	}
	@Override
	public int calRent(int days) {
		// TODO Auto-generated method stub
		return tonnage*days;
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值