JAVA基础day09

package com.atguigu.exer;

public class Circle extends GeometricObject{
private double radius;

public Circle(){

// this.color = “white”;
// this.weight = 1.0;
this.radius = 1.0;
}
public Circle(double radius){
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;
}

// //重写equals()方法和toString()
// public boolean equals(Object obj){
// if(obj == this)
// return true;
// else if(obj instanceof Circle){
// Circle c1 = (Circle)obj;
// return this.radius == c1.radius;
// }else{
// return false;
// }
// }
// public String toString(){
// //return radius + “”;
// return String.valueOf(radius);
// }

@Override
public boolean equals(Object obj) {
	if (this == obj)
		return true;
	if (obj == null)
		return false;
	if (getClass() != obj.getClass())
		return false;
	Circle other = (Circle) obj;
	if (Double.doubleToLongBits(radius) != Double
			.doubleToLongBits(other.radius))
		return false;
	return true;
}
@Override
public String toString() {
	return "Circle [radius=" + radius + "]";
}

}

package com.atguigu.exer;

public class GeometricObject {
protected String color;
protected double weight;

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

public 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 com.atguigu.exer;
/*

  • 编写一个类实现银行账户的概念,包含的属性有“帐号”、“密码”、“存款余额”、“利率”、“最小余额”,定义封装这些属性的方法。账号要自动生成。
    编写主类,使用银行账户类,输入、输出3个储户的上述信息。
    考虑:哪些属性可以设计成static属性。

*/
public class TestAccount {
public static void main(String[] args) {
Account a1 = new Account(“abc123”,1000);
Account a2 = new Account(“abc456”,2000);
Account a3 = new Account(“abc456”,2000);
System.out.println(a1);
System.out.println(a2);
System.out.println(a3);
}
}
class Account{
private int id;//账号
private String password;//密码
private double balance;//余额
private static double rate = 0.05;//利率
private static double minBalance = 1;//最小余额
private static int init = 1000;

public Account(String password,double balance){
	this.id = init++;
	this.password = password;
	this.balance = balance;
}

public int getId() {
	return id;
}
public void setId(int id) {
	this.id = id;
}
public String getPassword() {
	return password;
}
public void setPassword(String password) {
	this.password = password;
}
public double getBalance() {
	return balance;
}
public void setBalance(double balance) {
	this.balance = balance;
}
public static double getRate() {
	return rate;
}
public static void setRate(double rate) {
	Account.rate = rate;
}
public static double getMinBalance() {
	return minBalance;
}
public static void setMinBalance(double minBalance) {
	Account.minBalance = minBalance;
}

@Override
public String toString() {
	return "Account [id=" + id + ", password=" + password + ", balance="
			+ balance + "]";
}

}

package com.atguigu.exer;

public class TestCircle {
public static void main(String[] args) {
Circle c1 = new Circle(2.3);
Circle c2 = new Circle(2.3);
System.out.println(c1.equals(c2));
System.out.println(c1.toString());
}
}

package com.atguigu.exer;

/*

  • 请根据以下代码自行定义能满足需要的MyDate类,在MyDate类中覆盖equals方法,
  • 使其判断当两个MyDate类型对象的年月日都相同时,结果为true,否则为false。
  • public boolean equals(Object o)

*/
public class TestMyDate {
public static void main(String[] args) {
MyDate m1 = new MyDate(14, 3, 1976);
MyDate m2 = new MyDate(14, 3, 1976);

	if (m1 == m2) {
		System.out.println("m1==m2");
	} else {
		System.out.println("m1!=m2"); // m1 != m2
	}

	
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值