动手动脑
如果子函数的创建,父函数构造函数的实现情况
package homework;
class Grandparent
{
public Grandparent()
{
System.out.println("GrandParent Created.");
}
public Grandparent(String string)
{
System.out.println("GrandParent Created.String:" + string);
}
}
class Parent extends Grandparent
{
public Parent()
{
//super("Hello.Grandparent.");
System.out.println("Parent Created");
// super("Hello.Grandparent.");
}
}
class Child extends Parent
{
public Child()
{
System.out.println("Child Created");
}
}
public class text
{
public static void main(String args[])
{
Child c = new Child();
}
}
在执行子类的构造函数时必须先执行父类的构造函数,子类继承父类,首先要定义出一个父类才能定义子类
若不想函数继续被继承下去,可以在class前面加上final代表这是最后一代,无法被继续继承,声明的方法不允许被覆盖,变量不允许更改
public final class text
{
private final String detail;
private final String postCode;
public text()
{
this.detail = "";
this.postCode = "";
}
public text(String detail , String postCode)
{
this.detail = detail;
this.postCode = postCode;
}
public String getDetail()
{
return this.detail;
}
public String getPostCode()
{
return this.postCode;
}
public boolean equals(Object obj)
{
if (obj instanceof text)
{
text ad = (text)obj;
if (this.getDetail().equals(ad.getDetail()) && this.getPostCode().equals(ad.getPostCode()))
{
return true;
}
}
return false;
}
public int hashCode()
{
return detail.hashCode() + postCode.hashCode();
}
package homework;
public class text {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(new A());
}
}
class A{}


用insterdof来判断两种类型之间是否可以进行相互转化
package homework;
public class text
{
public static void main(String[] args)
{
Object hello = "Hello";
System.out.println("Object对象可以被转换为Object" + (hello instanceof Object));
System.out.println("Object对象可以被转换为String" + (hello instanceof String));
System.out.println("Object对象可以被转换为Math" + (hello instanceof Math));
System.out.println("Object对象可以被转换为Comparable" + (hello instanceof Comparable));
String a = "Hello";
}
}

子类可以赋值给父类,反之则不行
package homework;
class Mammal{}
class Dog extends Mammal {}
class Cat extends Mammal{}
public class text
{
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 homework;
public class text {
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);
}
}


浙公网安备 33010602011771号