内部类的使用以及抽象类的实现


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 *** ...

   子类必须实现所有的抽象方法!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值