一些瑞格java接口或抽象类的题

在这里插入图片描述

import java.util.*;

class Dog{
    String breed,color,spot;
    int weight;
	
	public void show(){
		System.out.println("这是一只"+breed+"体重为"+weight+",颜色为"+color);
	}
}

class Sdog extends Dog{
	public void show(){
		System.out.println("这是一只"+breed+"体重为"+weight+",颜色为"+color);
		System.out.println("这是一只"+breed+",颜色为"+color+",斑点颜色为"+spot);
	}
}

class Udog extends Dog{
	public void show(){
		System.out.println("这是一只"+breed+"犬");
	}
}

public class Main {
    public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String breed,color,spot;
		int weight;
		breed = in.next();
		weight = in.nextInt();
		color = in.next();
		spot = in.next();
		Sdog d1=new Sdog();
		d1.breed=breed;
		d1.weight=weight;
		d1.color=color;
		d1.spot=spot;
		d1.show();
		breed=in.next();
		weight=in.nextInt();
		color=in.next();
		Udog d2 = new Udog();
		d2.breed=breed;
		d2.show();
	}
}

在这里插入图片描述

import java.util.*;

class Car{
    int wheel,men;
	double weight,limit;
	
	public Car(int a,double b){
		this.wheel=a;
		this.weight=b;
	}

	public void show(){
		System.out.println("汽车:");
		System.out.println("轮子数:"+wheel+"个");
		System.out.println("自身重量:"+weight+"吨");
	}
}

class Sedan extends Car{
	public Sedan(int a,double b,int c){
		super(a,b);
		this.men=c;
	}
	public void show(){
		System.out.println("小轿车:");
		System.out.println("轮子数:"+wheel+"个");
		System.out.println("自身重量:"+weight+"吨");
		System.out.println("额定乘客数:"+men+"人");
	}
}

class Truck extends Car{
	public Truck(int a,double b,int c,double d){
		super(a,b);
		this.men=c;
		this.limit=d;
	}
	public void show(){
		System.out.println("卡车:");
		System.out.println("轮子数:"+wheel+"个");
		System.out.println("自身重量:"+weight+"吨");
		System.out.println("额定乘客数"+men+"人");
		System.out.println("载重量"+limit+"吨");
	}
}

public class Main {
    public static void main(String[] args) {
		int wheel,men;
		double weight,limit;
		Scanner in = new Scanner(System.in);
		wheel=in.nextInt();
		weight=in.nextDouble();
		Car q1 = new Car(wheel,weight);
		q1.show();
		wheel=in.nextInt();
		weight=in.nextDouble();
		men=in.nextInt();
		Sedan w1 = new Sedan(wheel,weight,men);
		w1.show();
		wheel=in.nextInt();
		weight=in.nextDouble();
		men=in.nextInt();
		limit=in.nextDouble();
		Truck e1 = new Truck(wheel,weight,men,limit);
		e1.show();
	}
	
}

以下代码author:Haohui-W


在这里插入图片描述

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Employer employer = new BasicEmployer(scan.next(), scan.next(), scan.nextDouble());
        employer.showSalary();
        employer.showBonus();
        Employer employer1=new GoodEmployer(scan.next(), scan.next(), scan.nextDouble(), scan.nextDouble());
        employer1.showSalary();
        employer1.showBonus();

    }
}
abstract class Employer{
    private final String name;
    private final String department;
    private final double salary;

    public Employer(String name, String department, double salary) {
        this.name = name;
        this.department = department;
        this.salary = salary;
    }

    public void showSalary(){
        System.out.println("我叫" + name + ",在" + department + "部门" + ",我的工资是" + salary);
    }

    /**
     * 显示奖金
     */
    public abstract void showBonus();
}
class BasicEmployer extends Employer {

    public BasicEmployer(String name, String department, double salary) {
        super(name, department, salary);
    }

    @Override
    public void showBonus() {
        System.out.println("我是普通员工,没有奖金,加油升级!");
    }
}
class GoodEmployer extends Employer{
    private final double bonus;
    public GoodEmployer(String name, String department, double salary, double bonus) {
        super(name, department, salary);
        this.bonus = bonus;
    }

    @Override
    public void showBonus() {
        System.out.println("我是优秀员工,我的奖金是"+bonus);
    }
}		

在这里插入图片描述

import java.util.Scanner;

public class Main {
    static Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) {

        Teacher teacher = new Teacher();
        teacher.setLecture(new Lecture() {
            @Override
            public void lecture(String str) {
                System.out.println("我是英语老师,I say " + str);
            }
        });
        teachingRace(teacher);
        Teacher teacher1 = new Teacher();
        teacher1.setLecture(new Lecture() {
            @Override
            public void lecture(String str) {
                System.out.println("我是数学老师,I say " + str);
            }
        });
        teachingRace(teacher1);
    }
    public static void teachingRace(Teacher t){
        t.getLecture().lecture(scanner.next());
    }
}
interface Lecture {

    void lecture(String str);
}
class Teacher {
    private Lecture lecture;

    public Lecture getLecture() {
        return lecture;
    }

    public void setLecture(Lecture lecture) {
        this.lecture = lecture;
    }


}

在这里插入图片描述

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Animal cat = new Cat();
        cat.setName(scanner.next());
        Animal mouse= new Mouse();
        mouse.setName(scanner.next());
        cat.speak();
        mouse.speak();
    }
}
abstract class Animal{
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    private String name;
    public abstract void speak();
}
class Mouse extends Animal{

    @Override
    public void speak() {
        System.out.println(super.getName() + "的叫声为吱吱");
    }
}
class Cat extends Animal{

    @Override
    public void speak() {
        System.out.println(super.getName() + "的叫声为喵喵");
    }
}

在这里插入图片描述

public class Main {
    public static void main(String[] args) {
        Computer computer = new Computer();
        computer.add(new Mouse());
        computer.add(new KeyBoard());
        computer.add(new Mic());
        computer.powerOn();
        computer.powerOff();
    }
}
interface Usb{
    void turnOn();
    void turnOff();

}
class Mouse implements Usb{

    @Override
    public void turnOn() {
        System.out.println("鼠标启动了");
    }

    @Override
    public void turnOff() {
        System.out.println("鼠标关闭了");
    }
}
class KeyBoard implements Usb{

    @Override
    public void turnOn() {
        System.out.println("键盘启动了");
    }

    @Override
    public void turnOff() {
        System.out.println("键盘关闭了");
    }
}
class Mic implements Usb {

    @Override
    public void turnOn() {
        System.out.println("麦克启动了");
    }

    @Override
    public void turnOff() {
        System.out.println("麦克关闭了");
    }
}
class Computer {
    private Usb[] usbs=new Usb[10];

    public Usb[] getUsbs() {
        return usbs;
    }

    public void setUsbs(Usb[] usbs) {
        this.usbs = usbs;
    }

    void add(Usb usb){
        for (int i = 0; i < 10; i++) {
            if(usbs[i]==null){
                usbs[i]=usb;
                break;
            }
        }
    }
    void powerOn(){
        for (int i = 0; i < 10; i++) {
            if (usbs[i] != null) {
                usbs[i].turnOn();
            }
        }
        System.out.println("计算机开机成功");
    }
    void powerOff(){
        for (int i = 0; i < 10; i++) {
            if (usbs[i] != null) {
                usbs[i].turnOff();
            }
        }
        System.out.println("计算机关机成功");
    }
}

在这里插入图片描述

import java.util.Scanner;
interface ComputerAverage {  
   //start
     //write your code between start and  end,do not  delete any code
     double average(double[] data);
      //end 
}  
class Gymnastics implements ComputerAverage {  
  public double average(double []data) { 
      double sum=0;  
      double temp;
       //start
     //write your code between start and  end,do not  delete any code
     temp=data[0];
        for (double datum : data) {
            sum+=datum;
            if(datum>temp) {
                temp=datum;
            }
        }
        sum-=temp;
        for (double datum : data) {
            if (datum<temp) {
                temp=datum;
            }
        }
        sum-=temp;
        return sum/(data.length-2);
      //end 
  }  
}  
class School implements ComputerAverage{  
   //start
     //write your code between start and  end,do not  delete any code
     public double average(double[] data) {
        double sum=0;
        for (double datum : data) {
            sum+=datum;
        }
        sum/=data.length;
      //end 
      return sum;  
  }  
}
public class Main {
  public static void main(String[] args) {  
      Scanner scan=new Scanner(System.in);
	  int n=scan.nextInt();  
      double []data=new double[n];  
      for(int i=0;i<n;i++){  
          data[i]=scan.nextDouble();  
      }   
      //start
     //write your code between start and  end,do not  delete any code
     ComputerAverage g=new Gymnastics();
      //end      
      System.out.print("Gymnastics average is:");
      System.out.printf("%.2f\n",g.average(data));  
      //start
     //write your code between start and  end,do not  delete any code
     g=new School();
      //end
      System.out.print("School average is:");  
      System.out.printf("%.2f",g.average(data));
  }  
}

在这里插入图片描述


public class Main {
    public static void main(String[] args) {
        Printer printer = new Printer();
        printer.print(new A4Paper());
        printer.print(new A6Paper());
    }
}
interface Paper{
    String getName();
}
class Printer {
    public void print(Paper p){
        System.out.println(p.getName());
    }
}
class A4Paper implements Paper{
    @Override
    public String getName() {
        return "A4";
    }
}
class A6Paper implements Paper{

    @Override
    public String getName() {
        return "A6";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值