package test01;
public final class Address
{
private final String detail;
private final String postCode;
//在构造方法里初始化两个实例属性
public Address()
{
this.detail = "";
this.postCode = "";
}
public Address(String detail , String postCode)
{
this.detail = detail;
this.postCode = postCode;
}
//仅为两个实例属性提供getter方法
public String getDetail()
{
return this.detail;
}
public String getPostCode()
{
return this.postCode;
}
//重写equals方法,判断两个对象是否相等。
public boolean equals(Object obj)
{
if (obj instanceof Address)
{
Address ad = (Address)obj;
if (this.getDetail().equals(ad.getDetail()) && this.getPostCode().equals(ad.getPostCode()))
{
return true;
}
}
return false;
}
public int hashCode()
{
return detail.hashCode() + postCode.hashCode();
}
}//加上final后,是为不可变类
package test01;
class Mammal{}
class Dog extends Mammal {}
class Cat extends Mammal{}
public class TestCast
{
public static void main(String args[])
{
Mammal m;
Dog d=new Dog();
Cat c=new Cat();
m=d;
//d=m;
d=(Dog)m;
//d=c;
//c=(Cat)m;
}
}//注释部分出错,不同类的对象之间的赋值需要用类似于强制类型转换的方法,且只能由大转小
package test01;
class Parent{
public int myValue = 100;
public void printValue(){
System.out.println("Parent.printValue(),myValue="+myValue);
}
}
class Child extends Parent{
public int myValue = 200;
public void printValue(){
System.out.println("Child.printValue(),myValue="+myValue);
}
}
public class Main{
public static void main(String[] args) {
Parent p = new Parent();
Child c = new Child();
Parent a = new Child();
p.printValue();
c.printValue();
a.printValue();
}
}
/*
Parent.printValue(),myValue=100
Child.printValue(),myValue=200
Child.printValue(),myValue=200
new谁,便优先调用谁里面的函数*/
package test01;
public class ParentChildTest {
public static void main(String[] args) {
Parent parent=new Parent();
parent.printValue();
Child child=new Child();
child.printValue();
parent=child;
parent.printValue();
parent.myValue++;
parent.printValue();
((Child)parent).myValue++;
parent.printValue();
}
}
class Parent{
public int myValue=100;
public void printValue() {
System.out.println("Parent.printValue(),myValue="+myValue);
}
}
class Child extends Parent{
public int myValue=200;
public void printValue() {
System.out.println("Child.printValue(),myValue="+myValue);
}
}
/*
Parent.printValue(),myValue=100
Child.printValue(),myValue=200
Child.printValue(),myValue=200
Child.printValue(),myValue=200
Child.printValue(),myValue=201
类之前也需要类似于强制类型转换的操作,才能达到预期效果
*/
package test01;
import java.util.Vector;
public class Zoo {
public static void main(String args[]) {
Feeder f = new Feeder("小李");
Vector<Animal> ans = new Vector<Animal>();
//饲养员小李喂养一只狮子
ans.add(new Lion());
//饲养员小李喂养十只猴子
for (int i = 0; i < 10; i++) {
ans.add(new Monkey());
}
//饲养员小李喂养5只鸽子
for (int i = 0; i < 5; i++) {
ans.add(new Pigeon());
}
f.feedAnimals(ans);
}
}
class Feeder {
public String name;
Feeder(String name) {
this.name = name;
}
public void feedAnimals(Vector<Animal> ans) {
for (Animal an : ans) {
an.eat();
}
}
}
abstract class Animal {
public abstract void eat();
}
class Lion extends Animal {
public void eat() {
System.out.println("我不吃肉谁敢吃肉!");
}
}
class Monkey extends Animal {
public void eat() {
System.out.println("我什么都吃,尤其喜欢香蕉。");
}
}
class Pigeon extends Animal {
public void eat() {
System.out.println("我要减肥,所以每天只吃一点大米。");
}
}
/*
我不吃肉谁敢吃肉!
我什么都吃,尤其喜欢香蕉。
我什么都吃,尤其喜欢香蕉。
我什么都吃,尤其喜欢香蕉。
我什么都吃,尤其喜欢香蕉。
我什么都吃,尤其喜欢香蕉。
我什么都吃,尤其喜欢香蕉。
我什么都吃,尤其喜欢香蕉。
我什么都吃,尤其喜欢香蕉。
我什么都吃,尤其喜欢香蕉。
我什么都吃,尤其喜欢香蕉。
我要减肥,所以每天只吃一点大米。
我要减肥,所以每天只吃一点大米。
我要减肥,所以每天只吃一点大米。
我要减肥,所以每天只吃一点大米。
我要减肥,所以每天只吃一点大米。
*/