package zhj_test;
public class Point {
public Point() {
//TODO
}
public Point(String Name, String address) {
this.aName = Name;
Point.aAddress = address;
}
public String toString() {
return this.getClass().getName() + "[" + getName() + ", " + Point.aAddress + "]";
}
public void setName(final String name) {
this.aName = name;
}
public String getName() {
return aName;
}
private String aName;
public static String aAddress;
static {
aAddress = "BeiJing";
}
{
aName = "asdfasdfasfadsf";
}
public static void print(Object obj) {
System.out.println(obj);
}
public static void main(String[] args) {
Point p = new Point();
Point.print(p);
Point p1 = new Point("Yoyo", "ShenYang");
Point.print(p);
Point.print(p1);
Person[] ps = new Person[2];
ps[0] = new Manager("zhj", "Beijing", "1010", 32);
ps[1] = new Employee("test", "Beijing", "1010", 6);
for (Person s : ps) {
System.out.println(s.toString());
}
}
abstract public static class Person {
public Person() {
}
public Person(int age) {
this.setAge(age);
}
public abstract String getName();
public abstract String getAddress();
public abstract String getCode();
public abstract String toString();
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private int age;
}
public static class Manager extends Person {
public Manager() {
//TODO
}
public Manager(String name, String address, String code, int age) {
super.setAge(age);
this.aName = name;
this.aAddress = address;
this.aCode = code;
}
public String getName() {
return aName;
}
public String getAddress() {
return aAddress;
}
public String getCode() {
return aCode;
}
public String toString() {
return this.getClass().getName() + "[Name=" + this.getName() + ", age=" + this.getAge() +
", address=" + this.getAddress() + ", Code=" + this.getCode();
}
private String aName;
private String aAddress;
private String aCode;
}
public static class Employee extends Person {
public Employee() {
//TODO
}
public Employee(String name, String address, String code, int age) {
super.setAge(age);
this.aName = name;
this.aAddress = address;
this.aCode = code;
}
public String getName() {
return aName;
}
public String getAddress() {
return aAddress;
}
public String getCode() {
return aCode;
}
public String toString() {
return this.getClass().getName() + "[Name=" + this.getName() + ", age=" + this.getAge() +
", address=" + this.getAddress() + ", Code=" + this.getCode();
}
private String aName;
private String aAddress;
private String aCode;
}
}
说明:
1> 内部类的实现如上代码所示,由于在Java中,类中的静态方法不能直接调用动态方法。只有将内部类修饰为静态类(public static ***),然后才能够在静态类中调用该类的成员变量与成员方法。
2> 抽象类:关键字abstract(超类的前部,以及超类中的抽象方法,必须含有此关键字),
方式:
抽象类 :abstract class ***。
抽象方法:public abstract String *** ...
子类必须实现所有的抽象方法!