Exercícios Java 15.1 Classes e interfaces abstratas

Exercícios Java 15.1 Classes e interfaces abstratas


Preste atenção à ambição da conta pública e responda a "11.23" para obter o arquivo original

Parte 1: aula abstrata

1. Defina uma classe Shape:
Contém atributos: Radius
contém 1 método: cubage () para encontrar o volume; o
método cubage é abstrato.
Três subclasses são derivadas da classe Shape:
Globe, Cylinder e Cone, que implementam o método cubage respectivamente.
Execute o teste e, quando o valor necessário for fornecido, encontre o volume da forma correspondente.

método um:

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

Método dois:

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. Defina uma classe abstrata de veículo motorizado Motovercal, que tem o número da placa, tipo de tipo, atributos de preço e preço, e há um método show () que é um método abstrato. Defina um carro Classe de carro, que tem um único atributo color, e um Bus, possui um atributo único seatCount, que realiza a função conforme mostrado na figura:

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+"个座位");		
	}
}

Parte 2: Interface

1. Implementar a interface

Defina uma interface Usb, que possui o método de trabalho work (), defina um mouse Usb UsbMouse e um teclado UsbKeyborder, deixe os dois dispositivos Usb herdarem a interface Usb, o resultado da operação é o seguinte:
interface interface

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. Escreva programas Java conforme necessário:

(1) Escreva uma interface: InterfaceA, que contém apenas um método int method (int n);

(2) Escreva uma classe: ClassA para implementar a interface InterfaceA, ao implementar o método de interface do método int (int n), é necessário calcular a soma de 1 para n;

(3) Escreva outra classe: ClassB para implementar a interface Interface A. Ao implementar o método de interface do método int (int n), é necessário calcular o fatorial de n (n!);

(4) Escreva classes de teste.

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

Parte 3: Herdar a classe e implementar a interface

Cavalo herda Animal, e também pode realizar a habilidade de voar, conhecida como Pégaso, os resultados da corrida são os seguintes:

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("小马嘟嘟,是一只会飞的飞马");
		
	}
	
}

Acho que você gosta

Origin blog.csdn.net/m0_46653702/article/details/109827844
Recomendado
Clasificación