天天看点

关于父类转子类例子

public class Father {

 private String name;

 private String title;

 public String getName() {

  return name;

 }

 public void setName(String name) {

  this.name = name;

 }

 public String getTitle() {

  return title;

 }

 public void setTitle(String title) {

  this.title = title;

 }

}

public class Child extends Father {

 private int age;

 public int getAge() {

  return age;

 }

 public void setAge(int age) {

  this.age = age;

 }

}

public class Test {

 public static void main(String[] args) {

  Child c=new Child();

  c.setName("c");

  c.setAge(2);

  Father f=c;

  f.setTitle("t");

  Child d=(Child)f;

  System.out.println(d.getAge());

 }

}