Java核心技术第五章——继承

  • 一个对象变量(例如, 变量e ) 可以指示多种实际类型的现象被称为多态
  • 在运行时能够自动地选择调用哪个方法的现象称为动态绑定
  • 当一个类被声明为final,表示这个类不允许被继承,这个类的方法默认都为final,但是数据域不默认为final。
  • 只要有一个抽象方法,这个类就是抽象类,抽象类不能创建实例对象
public boolean equals(Object otherObject)
{
// a quick test to see if the objects are identical
if (this == otherObject) return true;
// must return false if the explicit parameter is null
if (otherObject == null ) return false;
// if the classes don' t match, they can' t be equal
if (getClass() != otherObject.getClass())
	return false;
// now we know otherObject is a non-null Employee
Employee other = (Employee) otherObject;
// test whether the fields have identical values
return name.equals(other.name)
&& salary = other , sal ary
&& hi reDay. equals(other, hi reDay) :
//为了防备name或hireDay可能为null的情况需要使用Objects.equals方法。
//如果两个参数都为null,Objects.equals(a,b) 调用将返回true
}
  • 强烈建议为自定义的每一个类增加toString 方法。这样做不仅自己受益, 而且所有使用这个类的程序员也会从这个日志记录支持中受益匪浅

对象包装器与自动装箱

ArrayList<Integer> arrayList = new ArrayList<>();
arrayList.add(1);//等价于arrayList.add(Integer.valueOf(1)),这就是自动装箱
int a = arrayList.get(0);//自动拆箱
ArrayList<Double[]> arrayList2 = new ArrayList<>();
Double[] bDoubles = new Double[2];
bDoubles[0] = 1.0;
bDoubles[1] = 2.0;
arrayList2.add(bDoubles);
for(Double[] b:arrayList2) {
	System.out.println(Arrays.deepToString(b));
}
public static void print(double ... s) {
	for(double e:s) {
		System.out.println(e);
	}
}
print(1.2,1.0);

枚举的应用

编写优化算法代码把所有的参数写到枚举里面

enum psoParameter{
	c1(2),c2(2),w(0.1);
	double i; 
	private parameter(double i) {
		this.i = i;
	}
	public double getValue() {
		return i;
	}
}
System.out.println(psoParameter.c1.getValue());

更多用法参考Java枚举使用场景

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值