2020/11/04 第四次上机作业

课后作业第二题

package asd;

interface Shape{
	double area(double a);
}

class Square implements Shape{
	public double area(double l) {
		return l*l;
	}
}

class Circle implements Shape{
	public double area (double r) {
		return Math.PI*r*r;
	}
}
public class Test {
	public static void main(String[] args) {
		Shape sq=new Square();
		System.out.println("正方形的面积为:"+sq.area(2));
		Shape ci=new Circle();
		System.out.println("圆形的面积为:"+ci.area(3));
	}
}

雇员实例化:

package asd;

abstract class Empolyee{
	 String name;
	 int number;
	 double salary;
	 abstract void work();
	public Empolyee(String name,int number,double salary) {
		this.name=name;
		this.number=number;
		this.salary=salary;
	}
}

class Programmer extends Empolyee{
	public Programmer(String name,int number,double salary) {
		super(name,number,salary);
	}
	void work() {
		System.out.println("I am writing code!");
	}
}

class ProjectManager extends Empolyee{
	double bonus; 
	public ProjectManager(String name,int number,double salary,double bonus) {
		super(name,number,salary);
		this.bonus=bonus;
	}
	void work() {
		System.out.println("I am on a mission!");
	}
}
public class Demo {
	public static void main(String[] args) {
		Empolyee pr=new Programmer("小石头",19,1024.1);
		pr.work();
		Empolyee pro=new ProjectManager("大石头",20,2048.1,1024.0);
		pro.work();
	}
}

匿名

发表评论

匿名网友