面向对象(中)-Object类综合练习
package top.qaqaq.java.P302;

public class GeometricObject {

	protected String color;
	protected double weight;

	protected GeometricObject() {
		super();
		this.color = "white";
		this.weight = 1.0;
	}

	protected GeometricObject(String color, double weight) {
		super();
		this.color = color;
		this.weight = weight;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public double getWeight() {
		return weight;
	}

	public void setWeight(double weight) {
		this.weight = weight;
	}

}
package top.qaqaq.java.P302;

public class Circle extends GeometricObject {

	private double radius;

	public Circle() {
		super();
		radius = 1.0;
//		color = "white";
//		weight = 1.0;
	}

	public Circle(double radius) {
		super();
		this.radius = radius;
	}

	public Circle(double radius, String color, double weight) {
		super(color, weight);
		this.radius = radius;
	}

	public double getRadius() {
		return radius;
	}

	public void setRadius(double radius) {
		this.radius = radius;
	}

	// 求圆的面积
	public double findArea() {
		return 3.14 * radius * radius;
	}

	// 比较两个圆的半径是否相等,如相等,返回true。
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}

		if (obj instanceof Circle) {
			Circle c = (Circle) obj;
			return this.radius == c.radius;
		}

		return false;
	}

	@Override
	public String toString() {
		return "Circle [radius=" + radius + "]";
	}

}
package top.qaqaq.java.P302;
/*
 * 写一个测试类,创建两个Circle对象,判断其颜色是否相等;
 * 利用equals方法判断其半径是否相等;利用toString()方法输出其半径。
 * 
 * 
 * 
 */
public class CircleTest {
	
	public static void main(String[] args) {
		
		Circle circle1 = new Circle(2.3);		
		Circle circle2 = new Circle(3.3, new String("white"), 2.0);
		
		System.out.println("颜色是否相等:" + circle1.getColor().equals(circle2.getColor()));
		
		System.out.println("半径是否相等:" + circle1.equals(circle2));
		
		System.out.println(circle1);
		System.out.println(circle2.toString());
	}

}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇