Java第五章作业
程序员文章站
2022-04-02 23:01:16
...
1
import java.util.Scanner;
abstract class Animal{
abstract void cry();
abstract void getAnimalName();
}
class Dog extends Animal{
void getAnimalName(){
System.out.print("现在播放狗类的声音:");
}
void cry(){
System.out.println("汪汪...汪汪");
}
}
class Cat extends Animal{
void getAnimalName(){
System.out.print("现在播放猫类的声音:");
}
void cry(){
System.out.println("喵喵...喵喵");
}
}
class Simulator{
void playSound(Animal animal){
animal.getAnimalName();
animal.cry();
}
}
public class Main{
public static void main(String args[]) {
Simulator simulator = new Simulator();
Scanner scan=new Scanner(System.in);
String a=scan.next();
if(a.equals("dog"))
simulator.playSound(new Dog());
if(a.equals("cat"))
simulator.playSound(new Cat());
}
}
2
import java.util.Scanner;
abstract class Car{
abstract void getInfor();
}
class BMW extends Car{
void getInfor(){
System.out.println("This is BMW");
}
}
class Das extends Car{
void getInfor(){
System.out.println("This is Das");
}
}
class Maze extends Car{
void getInfor(){
System.out.println("This is Maze");
}
}
class AA{
void getCar(Car car){
car.getInfor();
}
}
public class Main {
public static void main(String[] args) {
Scanner up=new Scanner(System.in);
AA aa=new AA();
String a=up.next();
if(a.equalsIgnoreCase("B")){
aa.getCar(new BMW());
}
if(a.equalsIgnoreCase("D")){
aa.getCar(new Das());
}
if(a.equalsIgnoreCase("M")){
aa.getCar(new Maze());
}
}
}
3
import java.util.Scanner;
abstract class Person {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public abstract void showName();
}
class Student extends Person{
public void setName(String name){
this.name=name;
}
public void showName(){
System.out.println("I name is:"+name);
}
}
public class Main {
public static void main(String[] args) {
String name;
Scanner scanner = new Scanner(System.in);
name = scanner.next();
Student s = new Student();
s.setName(name);
s.showName();
}
}
4
import java.util.Scanner;
interface Muitiplication{
public double mult(double a,double b);
}
class MuitiplicationImpl implements Muitiplication{
public double mult(double a,double b){
return a*b;
}
}
class MuitiplicationImpls implements Muitiplication{
public double mult(double a,double b){
return a+b;
}
}
public class Main {
public static void main(String[] args) {
Scanner sca = new Scanner(System.in);
double i = sca.nextDouble();
MuitiplicationImpl m = new MuitiplicationImpl();
System.out.println(m.mult(i,i));
MuitiplicationImpls ms = new MuitiplicationImpls();
System.out.println(ms.mult(i,i));
}
}
上一篇: VScode远程Linux开发C/C++
下一篇: 第五章 常用模块