Javase day05_作业

作业

/**
	 * 1、定义一个学生类,包括属性:学号(ID),姓名(name),
	 * 成绩(score);构造方法(带三个参数);每个属性的访问器方法。
	 *
	 */
public class Students {
private int ID;
private String name;
private int score;
public Students() {
	super();
}
public Students(int iD, String name, int score) {
	super();
	ID = iD;
	this.name = name;
	this.score = score;
}
public int getID() {
	return ID;
}
public void setID(int iD) {
	ID = iD;
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public int getScore() {
	return score;
}
public void setScore(int score) {
	this.score = score;
}

}
@Test 
public void myTest01(){
	Students s=new Students(1234,"yunjiaen",150);
	System.out.println(s.getID()+" "+s.getName()+" "+s.getScore());
	}
/**
 * 2、创建类A1,实现构造方法中输出This is A;创建A1的子类B1,
 * 实现构造方法中输出This is B;创建B1的子类C1,实现构造方法中输出This is C。  
 */


public class A1 {

	public A1() {
		super();
		System.out.println("This is A");
	}	
}
public class B1  extends A1{
	public B1() {
		super();
		System.out.println("This is B");
	}
}
public class C1 extends B1 {
	public C1() {
		super();
		System.out.println("This is C");
	}
}
@Test 
public void myTest02(){
	A1 a=new A1();
	B1 b=new B1();
	C1 c=new C1();
}
/**
 * 3、设计四个类,分别如下: [必做题] 
 * 3.1 设计Shape表示图形类,
 * 有面积属性area、周长属性per,颜色属性color,有两个构造方法
 * (一个是默认的、一个是为颜色赋值的),还有3个抽象方法,分别是:getArea计算面积、
 * getPer计算周长、showAll输出所有信息,还有一个求颜色的方法getColor。 
 * 3.2 设计 2个子类: 3.2.1 Rectangle表示矩形类,增加两个属性,Width表示长度、
 * height表示宽度,重写getPer、getArea和showAll三个方法,另外又增加一个构造方法
 * (一个是默认的、一个是为高度、宽度、颜色赋值的)。 3.2.2 Circle表示圆类,增加1个属性
 * ,radius表示半径,重写getPer、getArea和showAll三个方法,另外又增加两个构造方法(为半径、
 * 颜色赋值的)。 
 * 3.3 测试类中,在main方法中,
 * 声明创建每个子类的对象,并调用2个子类的showAll方法。
 */
public abstract class Shape {
private double area;
private double per;
private String color;
public Shape() {
	this.area=0;
	this.per=0;
	this.color="白色";
}
public Shape(String color) {
	super();
	this.color = color;
}
public String getColor() {
	return color;
}
public void setColor(String color) {
	this.color = color;
}
public abstract double getArea();
public abstract double getPer();
public abstract void showAll();
}
public  class Rectangle extends Shape {
private double Width;
private double height;
public Rectangle() {
	this.Width=0;
	this.height=0;
}
public Rectangle(String color,double width, double height) {
	setColor(color);
	this.Width = width;
	this.height = height;
}
@Override
public double getArea() {
	double area=this.Width*this.height;
    return area;
}
@Override
public double getPer() {
	double per=2*(this.Width+this.height);
    return per; 
}

@Override
public void showAll() {
	System.out.println("图形为矩形");
	System.out.println("长度为:"+this.height);
	System.out.println("宽度为:"+this.Width);
	System.out.println("面积为:"+this.getArea());
	System.out.println("周长为:"+this.getPer());
	System.out.println("颜色为:"+this.getColor());
}
}
public class Circle extends Shape {
	private double radius;
	public Circle() {
		super();
		radius=0;
	}
	public Circle(String color,double radius) {
		setColor(color);
		this.radius = radius;
	}
	@Override
	public double getArea() {
		double area=3.14*this.radius*this.radius;
	    return area;
	}
	@Override
	public double getPer() {
		double per=2*this.radius*3.14;
	    return per; 
	}

	@Override
	public void showAll() {
		System.out.println("图形为矩形");
		System.out.println("半径为:"+this.radius);
		System.out.println("面积为:"+this.getArea());
		System.out.println("周长为:"+this.getPer());
		System.out.println("颜色为:"+this.getColor());
	}
}
@Test 
public void myTest03() {
	Rectangle r=new Rectangle("蓝色",5.0,4.0);
	r.showAll();
	Circle c=new Circle("红色",3.0);
	c.showAll();
}
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值