Java SE 学习笔记03 代码

关于父类与子类在属性和方法调用的代码

 

Java代码 复制代码 收藏代码
  1. /**
  2. * 方法重写
  3. */
  4. package org.sean.module03;
  5. /**
  6. * 类变量指的是属于类,通过类名就有可以访问的变量
  7. *
  8. * 实例变量指的是属于类的实例,通过对象可以访问实例变量,但是通过类名不能访问实例变量
  9. *
  10. * 同样,静态方法属于一个类,通过类名就可以访问静态方法,而实例方法属于对象,通过对象可以访问
  11. *
  12. * 所谓隐藏:子类隐藏父类的属性和静态方法,通过子类对象,不能访问父类隐藏了的属性和静态方法(属性和静态方法不存在多态性)
  13. *
  14. * 但是将子类对象转换为父类对象之后,可以访问父类中被隐藏的属性和静态方法
  15. *
  16. * 所谓覆盖(重写): 子类能够覆盖父类的实例方法,通过子类对象,不能访问父类被覆盖了的实例方法
  17. *
  18. * 将子类转换为父类对象后,也不能访问父类中被覆盖的实例方法。
  19. *
  20. * 结论:属性和静态方法只能被隐藏,实例方法只能被覆盖(重写)
  21. *
  22. * @author 不落的太阳(Sean Yang)
  23. */
  24. class Parent2 {
  25. // 类变量,Parent的类别
  26. public static String kind = "org.sean.module03.CoverAndHide.Parent";
  27. // 类变量,Parent的年龄
  28. public static int age = 50;
  29. // 实例变量,Parent的名称
  30. public String name = "Parent";
  31. /** 静态方法,获得Parent的类别 */
  32. public static String getKind() {
  33. System.out.println("parent getKind() method called");
  34. return kind;
  35. }
  36. /** 静态方法,获得Parent的年龄 */
  37. public static int getAge() {
  38. System.out.println("parent getAge() method called");
  39. return age;
  40. }
  41. /** 实例方法,获得Parent的名称 */
  42. public String getName() {
  43. System.out.println("parent getName() method called");
  44. return this.name;
  45. }
  46. }
  47. class Child2 extends Parent2 {
  48. // 类变量,Child的类别
  49. public static String kind = "org.sean.module03.CoverAndHide.Child";
  50. // 类变量,Child的年龄
  51. public static int age = 25;
  52. // 实例变量,Child的名称
  53. public String name = "child";
  54. /** 静态方法,获得Child的类别 */
  55. public static String getKind() {
  56. System.out.println("child getKind() method called");
  57. return kind;
  58. }
  59. /** 静态方法,获得Child的年龄 */
  60. public static int getAge() {
  61. System.out.println("child getAge() method called");
  62. return age;
  63. }
  64. /** 实例方法,获得Child的名称 */
  65. public String getName() {
  66. System.out.println("child getName() method called");
  67. return this.name;
  68. }
  69. /** 实例方法,获得Parent的名称 */
  70. public String getParentName() {
  71. return super.name;
  72. }
  73. /** 静态方法,获得Parent的类别 */
  74. public static String getParentKind() {
  75. return Parent2.kind;
  76. }
  77. }
  78. public class CoverAndHide {
  79. @SuppressWarnings("static-access")
  80. public static void main(String[] args) {
  81. Child2 child = new Child2();
  82. System.out.println("child name is " + child.name + ", child age is "
  83. + child.age + " ,kind is " + child.kind);// 此处得到都是Child类的变量值
  84. /** 将Child类型转换为Parent类型,即多态情况 */
  85. // Parent2 parent = child;
  86. Parent2 parent = new Child2();
  87. System.out.println("转换为Parent之后的name is " + parent.name + ", age is "
  88. + parent.age + " ,kind is " + parent.kind);
  89. /**
  90. * 结论:
  91. *
  92. * 父类的实例变量和类变量能被子类的同名变量遮盖(隐藏),将子类对象转换为父类对象后,可以访问父类对象的实例变量和类变量
  93. *
  94. * 在子类中如果需要访问父类中被隐藏的实例变量,需要使用"super.属性名"
  95. *
  96. * 在子类中如果需要访问父类中被隐藏的类变量,需要使用"父类名.属性名"
  97. */
  98. System.out.println("子类访问父类被隐藏的实例变量:" + child.getParentName());
  99. System.out.println("子类访问父类被隐藏的类变量:" + child.getParentKind());
  100. /** 继承时方法的覆盖与隐藏问题 */
  101. child.getName();
  102. child.getKind();
  103. parent.getName();
  104. parent.getKind();
  105. /**
  106. * 结论:
  107. *
  108. * 父类的静态方法被子类的同名静态方法隐藏,而父类的实例方法被子类的实例方法覆盖(重写)
  109. *
  110. * 将子类转换为父类对象后(或者多态),可以访问父类的静态方法,但不能访问父类的实例方法
  111. */
  112. }
  113. }
/**
 * 方法重写
 */
package org.sean.module03;

/**
 * 类变量指的是属于类,通过类名就有可以访问的变量
 * 
 * 实例变量指的是属于类的实例,通过对象可以访问实例变量,但是通过类名不能访问实例变量
 * 
 * 同样,静态方法属于一个类,通过类名就可以访问静态方法,而实例方法属于对象,通过对象可以访问
 * 
 * 所谓隐藏:子类隐藏父类的属性和静态方法,通过子类对象,不能访问父类隐藏了的属性和静态方法(属性和静态方法不存在多态性)
 * 
 * 但是将子类对象转换为父类对象之后,可以访问父类中被隐藏的属性和静态方法
 * 
 * 所谓覆盖(重写): 子类能够覆盖父类的实例方法,通过子类对象,不能访问父类被覆盖了的实例方法
 * 
 * 将子类转换为父类对象后,也不能访问父类中被覆盖的实例方法。
 * 
 * 结论:属性和静态方法只能被隐藏,实例方法只能被覆盖(重写)
 * 
 * @author 不落的太阳(Sean Yang)
 */
class Parent2 {

	// 类变量,Parent的类别
	public static String kind = "org.sean.module03.CoverAndHide.Parent";
	// 类变量,Parent的年龄
	public static int age = 50;
	// 实例变量,Parent的名称
	public String name = "Parent";

	/** 静态方法,获得Parent的类别 */
	public static String getKind() {
		System.out.println("parent getKind() method called");
		return kind;
	}

	/** 静态方法,获得Parent的年龄 */
	public static int getAge() {
		System.out.println("parent getAge() method called");
		return age;
	}

	/** 实例方法,获得Parent的名称 */
	public String getName() {
		System.out.println("parent getName() method called");
		return this.name;
	}
}

class Child2 extends Parent2 {

	// 类变量,Child的类别
	public static String kind = "org.sean.module03.CoverAndHide.Child";
	// 类变量,Child的年龄
	public static int age = 25;
	// 实例变量,Child的名称
	public String name = "child";

	/** 静态方法,获得Child的类别 */
	public static String getKind() {
		System.out.println("child getKind() method called");
		return kind;
	}

	/** 静态方法,获得Child的年龄 */
	public static int getAge() {
		System.out.println("child getAge() method called");
		return age;
	}

	/** 实例方法,获得Child的名称 */
	public String getName() {
		System.out.println("child getName() method called");
		return this.name;
	}

	/** 实例方法,获得Parent的名称 */
	public String getParentName() {
		return super.name;
	}

	/** 静态方法,获得Parent的类别 */
	public static String getParentKind() {
		return Parent2.kind;
	}
}

public class CoverAndHide {

	@SuppressWarnings("static-access")
	public static void main(String[] args) {
		Child2 child = new Child2();
		System.out.println("child name is " + child.name + ", child age is "
				+ child.age + " ,kind is " + child.kind);// 此处得到都是Child类的变量值
		/** 将Child类型转换为Parent类型,即多态情况 */
		// Parent2 parent = child;
		Parent2 parent = new Child2();
		System.out.println("转换为Parent之后的name is " + parent.name + ", age is "
				+ parent.age + " ,kind is " + parent.kind);
		/**
		 * 结论:
		 * 
		 * 父类的实例变量和类变量能被子类的同名变量遮盖(隐藏),将子类对象转换为父类对象后,可以访问父类对象的实例变量和类变量
		 * 
		 * 在子类中如果需要访问父类中被隐藏的实例变量,需要使用"super.属性名"
		 * 
		 * 在子类中如果需要访问父类中被隐藏的类变量,需要使用"父类名.属性名"
		 */
		System.out.println("子类访问父类被隐藏的实例变量:" + child.getParentName());
		System.out.println("子类访问父类被隐藏的类变量:" + child.getParentKind());

		/** 继承时方法的覆盖与隐藏问题 */
		child.getName();
		child.getKind();
		parent.getName();
		parent.getKind();
		/**
		 * 结论:
		 * 
		 * 父类的静态方法被子类的同名静态方法隐藏,而父类的实例方法被子类的实例方法覆盖(重写)
		 * 
		 * 将子类转换为父类对象后(或者多态),可以访问父类的静态方法,但不能访问父类的实例方法
		 */
	}
}

 

 

关于父类和子类加载顺序的代码

 

Java代码 复制代码 收藏代码
  1. /**
  2. * 类的加载顺序
  3. */
  4. package org.sean.module03;
  5. /**
  6. * 父类的静态代码块--->子类的静态代码块--->父类的初始化块 --->父类的构造方法 --->子类的初始化块--->子类的构造方法
  7. *
  8. * 对于静态的部分,按照声明的先后顺序进行加载
  9. *
  10. * @author 不落的太阳(Sean Yang)
  11. */
  12. class Parent1 {
  13. @SuppressWarnings("unused")
  14. private int x = 50;
  15. static {
  16. System.out.println("parent static block");
  17. }
  18. @SuppressWarnings("unused")
  19. private static int sx = getNext(99);
  20. {
  21. System.out.println("parant init block");
  22. }
  23. public Parent1() {
  24. System.out.println("parent constructor");
  25. }
  26. public static int getNext(int base) {
  27. System.out.println("static parameter initialized");
  28. return ++base;
  29. }
  30. }
  31. class Child1 extends Parent1 {
  32. static {
  33. System.out.println("child static block");
  34. }
  35. {
  36. System.out.println("child init block");
  37. }
  38. public Child1() {
  39. System.out.println("child constructor");
  40. }
  41. }
  42. public class LoadingOrder {
  43. public static void main(String[] args) {
  44. @SuppressWarnings("unused")
  45. Child1 child = new Child1();
  46. }
  47. }
/**
 * 类的加载顺序
 */
package org.sean.module03;

/**
 * 父类的静态代码块--->子类的静态代码块--->父类的初始化块 --->父类的构造方法 --->子类的初始化块--->子类的构造方法
 * 
 * 对于静态的部分,按照声明的先后顺序进行加载
 * 
 * @author 不落的太阳(Sean Yang)
 */
class Parent1 {

	@SuppressWarnings("unused")
	private int x = 50;

	static {
		System.out.println("parent static block");
	}

	@SuppressWarnings("unused")
	private static int sx = getNext(99);

	{
		System.out.println("parant init block");
	}

	public Parent1() {
		System.out.println("parent constructor");
	}

	public static int getNext(int base) {
		System.out.println("static parameter initialized");
		return ++base;
	}
}

class Child1 extends Parent1 {

	static {
		System.out.println("child static block");
	}

	{
		System.out.println("child init block");
	}

	public Child1() {
		System.out.println("child constructor");
	}
}

public class LoadingOrder {

	public static void main(String[] args) {
		@SuppressWarnings("unused")
		Child1 child = new Child1();
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值