第一题
求几何图形的面积
/*
author:Kuo
*/
abstract class Shape { //几何图形
public abstract double getArea();
}
class Square extends Shape{
private double height=0; //正方形的边长
public Square(double height){
this.height=height;
}
public double getArea(){
return (this.height=this.height);
}
}
class Circle extends Shape{
private double r = 0; //圆的半径
private final static double PI = 3.14; //圆周率
public Circle(double r){
this.r = r;
}
public double getArea(){
return (PI*r*r);
}
}
测试入口
class TestShape{
public static void main(String[] args) {
Shape square=new Square(3);
Shape circle = new Circle(2);
System.out.println(square.getArea());
System.out.println(circle.getArea());
Shape sq=(Shape) circle;
System.out.println(sq.getArea());
}
}
运行结果
3.0
12.56
12.56
第二题
定义动物类,输出种类的属性
/*
author:Kuo
*/
abstract class Animal { //抽象类动物
private String color;
private String type;
private int age;
private int weight;
public String getColor(){
return color;
}
public void setColor(String color){
this.color=color;
}
public String getType(){
return type;
}
public void setType(String type){
this.type=type;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age=age;
}
public int getWeight(){
return weight;
}
public void setWeight(int weight){
this.weight=weight;
}
public abstract void info();
}
class Bird extends Animal { //小鸟继承动物
@Override
public String getColor(){
return super.getColor();
}
@Override
public void setColor(String color){
super.setColor(color);
}
@Override
public String getType(){
return super.getType();
}
@Override
public void setType(String type){
super.setType(type);
}
@Override
public int getAge(){
return super.getAge();
}
@Override
public void setAge(int age){
super.setAge(age);
}
@Override
public int getWeight(){
return super.getWeight();
}
@Override
public void setWeight(int weight) {
super.setWeight(weight);
}
@Override
public void info(){ //方法
System.out.println("我是一只"+getColor()+"的"+getType());
System.out.println("今年"+getAge()+"岁了");
}
}
class Fish extends Animal { //鱼继承动物
@Override
public String getColor(){
return super.getColor();
}
@Override
public void setColor(String color){
super.setColor(color);
}
@Override
public String getType(){
return super.getType();
}
@Override
public void setType(String type){
super.setType(type);
}
@Override
public int getAge(){
return super.getAge();
}
@Override
public void setAge(int age){
super.setAge(age);
}
@Override
public int getWeight(){
return super.getWeight();
}
@Override
public void setWeight(int weight) {
super.setWeight(weight);
}
@Override
public void info(){ //方法
System.out.println("我是一条"+getWeight()+"重的"+getType());
System.out.println("今年"+getAge()+"岁了");
}
}
测试输出
*/
class Test {
public static void main(String[] args) {
Animal animal=new Bird();
animal.setColor("红色");
animal.setType("鸟");
animal.setAge(4);
animal.info();
Animal animal1=new Fish();
animal1.setWeight(5);
animal1.setType("鱼");
animal1.setAge(2);
animal1.info();
}
}
运行结果
我是一只红色的鸟
今年4岁了
我是一条5重的鱼
今年2岁了
进程已结束,退出代码为 0
第三题
定义抽象打印机类,输出三种打印机
/*
author:kuo
*/
public abstract class Printer { //抽象类打印机
abstract void print();
}
class DotMatrixtPrinter extends Printer{ //针式打印机继承打印机类
void print(){ //输出针式打印
System.out.println("针式打印");
}
}
class InKpetPrinter extends Printer{ //喷墨打印继承打印机类
void print(){
System.out.println("喷墨打印");
}
}
class LaserPrinter extends Printer{ //激光打印继承打印机类
void print(){
System.out.println("激光打印");
}
}
测试输出
class example {
public static void main(String[] args) {
Printer pre = new DotMatrixtPrinter();
pre.print();
Printer prePrinter=new InKpetPrinter();
prePrinter.print();
Printer pre1=new LaserPrinter();
pre1.print();
}
}
运行结果
针式打印
喷墨打印
激光打印
进程已结束,退出代码为 0
第四题
定义接口PCI,实现各种功能输出
/*
author:kuo
*/
interface PCI {
void start();
void stop();
}
class NetworkCard implements PCI{
public void start(){
System.out.println("Sending data!");
}
public void stop()
{ System.out.println("Network Stop!");
}
}
class DisplayCard implements PCI{
public void start(){
System.out.println("DisplayCard images!");
}
public void stop(){
System.out.println("Stop DisplayCard!");
}
}
class SoundCard implements PCI {
public void start() {
System.out.println("Sound Start!");
}
public void stop(){
System.out.println("Sound Stop!");
}
}
测试输出
public class Test{
public static void usePCICard(PCI p){
p.start();
p.stop();
}
public static void main(String [] args) {
PCI dc=new DisplayCard();
usePCICard(dc);
PCI sc=new SoundCard();
usePCICard(sc);
PCI nc=new NetworkCard();
usePCICard(nc);
}
}
运行结果
DisplayCard images!
Stop DisplayCard!
Sound Start!
Sound Stop!
Sending data!
Network Stop!