Java演習15.1抽象クラスとインターフェース

Java演習15.1抽象クラスとインターフェース


パブリックアカウントの野心に注意し、「11.23」に返信して元のファイルを取得します

パート1:抽象クラス

1. Shapeクラスを定義し
ます。属性が含まれます:Radiusに
は1つのメソッドが含まれます:ボリュームを見つけるためのcubage(); cubage
メソッドは抽象です。
Shapeクラスから派生した3つのサブクラス:
Globe、Cylinder、およびConeは、それぞれcubageメソッドを実装します。
テストを実行し、必要な値が与えられたら、対応する形状の体積を見つけます。

方法1:

package com.shangjiti.aoian;
public class No1 {
    
    
	public static void main(String[] args) {
    
    
		Globe g=new Globe(3.0);
		System.out.println("半径为3.0的圆的体积为:"+g.cubage());
		Cylinder c=new Cylinder(3.0,3.0);
		System.out.println("半径为3.0,高度为3.0的圆锥的体积为:"+c.cubage());
		Cone cone=new Cone(3.0,3.0);
		System.out.println("半径为3.0,高度为3.0的圆柱的体积为:"+cone.cubage());
	}
}
abstract class Shape{
    
    
	double radius;
	public abstract double cubage();
}
class Globe extends Shape{
    
    
	public Globe(double r) {
    
    
		this.radius=r;
	}
	public double cubage() {
    
    
		 return Math.PI*Math.pow(radius, 3)*(double)4/3;
	}
}
class Cylinder extends Shape{
    
    
	double height;
	public Cylinder(double r,double h) {
    
    
		this.radius=r;
		this.height=h;
	}
	public double cubage(){
    
    
		return Math.PI*Math.pow(radius, 2)*((double)1/3)*height;		
	}	
}
class Cone extends Shape{
    
    
	double height;
	public Cone(double r,double h) {
    
    
		this.radius=r;
		this.height=r;		
	}
	public double cubage() {
    
    
		return Math.PI*Math.pow(radius, 2)*height;
	}
}

方法2:

package com.shangjiti.aoian;
public class No1 {
    
    
	public static void main(String[] args) {
    
    
		Globe g=new Globe(3.0);
		Cylinder c=new Cylinder(3.0,3.0);
		Cone cone=new Cone(3.0,3.0);
		g.cubage();
		c.cubage();
		cone.cubage();
	}
}
abstract class Shape{
    
    
	double radius;
	public abstract void cubage();
}
class Globe extends Shape{
    
    
	public Globe(double r) {
    
    
		this.radius=r;
	}
	public void cubage() {
    
    
		System.out.println("半径为"+radius+"的圆的体积为:"+Math.PI*Math.pow(radius, 3)*(double)4/3);
	}
}
class Cylinder extends Shape{
    
    
	double height;
	public Cylinder(double r,double h) {
    
    
		this.radius=r;
		this.height=h;
	}
	public void cubage(){
    
    
		System.out.println("半径为"+radius+",高度为"+height+"的圆锥的体积为:"+Math.PI*Math.pow(radius, 2)*((double)1/3)*height);		
	}	
}
class Cone extends Shape{
    
    
	double height;
	public Cone(double r,double h) {
    
    
		this.radius=r;
		this.height=r;		
	}
	public void cubage() {
    
    
		System.out.println("半径为"+radius+",高度为"+height+"的圆柱的体积为:"+Math.PI*Math.pow(radius, 2)*height);
	}
}

2.ナンバープレート番号、タイプタイプ、価格、および価格属性を持つ自動車Motovercalの抽象クラスを定義し、抽象メソッドであるshow()メソッドがあります。一意の車Carクラスを定義します。属性の色と1つのバスには、次の図に示すような機能を実現する一意の属性seatCountがあります。

package com.shangjiti.aoian;
public class No2 {
    
    
	public static void main(String[] args) {
    
    
		Car c=new Car("A00001","jeep",200000,"绿色");
		Bus b=new Bus("A00002","金龙",250000,30);
		c.show();
		b.show();
	}
}
abstract class Motovercal {
    
    
	String no;
	String type;
	double price;
	abstract void show();	
}
class Car extends Motovercal{
    
    
	String color;
	public Car(String no,String type,double price,String color) {
    
    
		this.no=no;
		this.type=type;
		this.price=price;
		this.color=color;		
	}
	void show() {
    
    
		System.out.println("本车车牌号为:"+no+",类型为:"+type+",价格为:"+price+",颜色为:"+color);		
	}
}
class Bus extends Motovercal{
    
    
	int seatCount;
	public Bus(String no,String type,double price,int seatCount) {
    
    
		this.no=no;
		this.type=type;
		this.price=price;
		this.seatCount=seatCount;		
	}
	void show() {
    
    
		System.out.println("本车车牌号为:"+no+",类型为:"+type+",价格为:"+price+",拥有:"+seatCount+"个座位");		
	}
}

パート2:インターフェース

1.インターフェースを実装します

作業方法の作業を(有するインターフェースUSBなど)を定義し、USBマウスUsbMouseとUsbKeyborderキーボードを定義する、2つのUSBデバイスは、USBインタフェースを継承させ、以下のように、演算結果がある:
インターフェースインターフェース

package com.shangjiti.aoian;
public class No3 {
    
    
	public static void main(String[] args) {
    
    
		UsbMouse m=new UsbMouse();
		m.work();
		UsbKeyborder k=new UsbKeyborder();
		k.work();
	}
}
interface Usb{
    
    
	void work(); //public abstract 可省略
}
class UsbMouse implements Usb{
    
    
	public void work() {
    
    
		System.out.println("鼠标开始工作了! 你可以移动鼠标了!");
	}
}
class UsbKeyborder implements Usb{
    
    
	public void work() {
    
    
		System.out.println("键盘开始工作了! 你可以敲字了!");
	}
}

2.必要に応じてJavaプログラムを作成します。

(1)インターフェイスを記述します。InterfaceA。これには、メソッドint method(int n);が1つだけ含まれています。

(2)クラスを記述します:インターフェースInterfaceAを実装するためのClassA。intメソッド(int n)インターフェースメソッドを実装する場合、1からnの合計を計算する必要があります。

(3)別のクラスを記述します:インターフェイスInterfaceAを実装するためのClassB。intメソッド(int n)インターフェイスメソッドを実装する場合、n(n!)の階乗を計算する必要があります。

(4)テストクラスを作成します。

package com.shangjiti.aoian;
public class No4 {
    
    
	public static void main(String[] args) {
    
    
		ClassA a=new ClassA();
		ClassB b=new ClassB();
		System.out.println(a.method(5));
		System.out.println(b.method(5));		
	}
}
interface InterfaceA{
    
    
	public abstract int method(int n);//public abstract 可省略
}
class ClassA implements InterfaceA{
    
    
	public int method(int n) {
    
    
		int sum = 0;
		for(int i=1;i<=n;i++)
			sum=sum+i;
		return sum;
//		if(n==1)
//			return 1;
//		else
//			return n+method(n-1);
	}	
}
class ClassB implements InterfaceA{
    
    
	public int method(int n) {
    
    	
		int p=1;
		for(int i=1;i<=n;i++)
			p=p*i;
		return p;
//		if(n==1)
//			return 1;
//		else
//			return n*method(n-1);
	}

パート3:クラスを継承し、インターフェースを実装する

馬は動物を継承し、ペガサスと呼ばれる飛ぶ能力を実現することもできます。実行結果は次のとおりです。

package com.shangjiti.aoian;
public class No5 {
    
    
	public static void main(String[] args) {
    
    
		Horse h=new Horse();
		h.fly();
	}
}
abstract class Animal{
    
    
	String name;
}
interface Flyable{
    
    
	public abstract void fly();
}
class Horse extends Animal implements Flyable{
    
    
	public void fly() {
    
    
		System.out.println("小马嘟嘟,是一只会飞的飞马");
		
	}
	
}

おすすめ

転載: blog.csdn.net/m0_46653702/article/details/109827844