day09_课后练习
代码阅读分析题
第1题
考核知识点:属性与多态无关
package com.atguigu.test01;
public class Test01 {
public static void main(String[] args) {
A a = new B();
System.out.println(a.num);
System.out.println(((B)a).num);
System.out.println(((A)((B)a)).num);
System.out.println("-------------------");
B b = new B();
System.out.println(b.num);
System.out.println(((A)b).num);
System.out.println(((B)((A)b)).num);
}
}
class A{
int num = 1;
}
class B extends A{
int num = 2;
}
/*
* 多态性现象:编译时类型与运行时类型不一致
* 但是多态性是针对方法来说,方法有动态绑定一说。
* 属性没有多态性。属性都是按照编译时类型处理的。
*/
public class Test01 {
public static void main(String[] args) {
A a = new B();
System.out.println(a.num);//a编译时类型就是A 1
System.out.println(((B)a).num);//编译后,因为a被强制成B类,是B类型 2
System.out.println(((A)((B)a)).num);//编译后,a转成B又转成A,是A类型 1
System.out.println("-------------------");
B b = new B();
System.out.println(b.num);//b编译时类型就是B 2
System.out.println(((A)b).num);//b被强制升级为A类型,按A类型处理, 1
System.out.println(((B)((A)b)).num);//b先转A又转B,最终是B类型 2
}
}
class A{
int num = 1;
}
class B extends A{
int num = 2;
}
第2题
考核知识点:实例初始化方法,属性与多态无关
package com.atguigu.test02;
public class Test02 {
public static void main(String[] args) {
Father f = new Son();
System.out.println(f.x);
}
}
class Father{
int x = 10;
public Father(){
this.print();
x = 20;
}
public void print(){
System.out.println("Father.x = " + x);
}
}
class Son extends Father{
int x = 30;
public Son(){
this.print();
x = 40;
}
public void print(){
System.out.println("Son.x = " + x);
}
}
package com.atguigu.test02;
/*
* 1、Father f = new Son();
* 实例初始化的过程:
* (1)父类的实例初始化
* <init>(){
* x = 10;//父类的x
* this.print();//子类的print,因为this代表的是正在创建的子类对象,而子类重写了print,所以是子类的print'
* System.out.println("Son.x = " + x);//子类的x,此时还没有赋值,那么是默认值x=0
x = 20;//父类的x
* }
* (2)子类的实例初始化
* <init>(){
* x = 30;//子类的x
* this.print();//子类的print
* System.out.println("Son.x = " + x);//子类的x,此时已经赋值x=30
x = 40;//子类的x
* }
*
* 2、执行System.out.println(f.x);
* 属性没有多态性,只看编译时类型,那么此时f.x表示父类的x
*/
public class Test02 {
public static void main(String[] args) {
Father f = new Son();
System.out.println(f.x);
}
}
class Father{
int x = 10;
public Father(){
this.print();
x = 20;
}
public void print(){
System.out.println("Father.x = " + x);
}
}
class Son extends Father{
int x = 30;
public Son(){
this.print();
x = 40;
}
public void print(){
System.out.println("Son.x = " + x);
}
}
第3题
考核知识点:多态,重写,实例初始化过程
package com.atguigu.test03;
public class Test03 {
public static void main(String[] args) {
Base b1 = new Base();
Base b2 = new Sub();
}
}
class Base {
Base() {
method(100);
}
public void method(int i) {
System.out.println("base : " + i);
}
}
class Sub extends Base {
Sub() {
super.method(70);
}
public void method(int j) {
System.out.println("sub : " + j);
}
}
package com.atguigu.test03;
/*
* 1、Base b1 = new Base();
* 父类的实例初始化,和子类无关
*
* <init>(){
* method(100);
* System.out.println("base : " + i); base:100
* }
*
* 2、Base b2 = new Sub();
* (1) 父类的实例初始化
*
* <init>(){
* method(100);//执行了子类重写的method()
* System.out.println("sub : " + j); sub:100
* }
*
* (2)子类的实例初始化
* <init>(){
* super.method(70);
* System.out.println("base : " + i); base:70
* }
*/
public class Test03 {
public static void main(String[] args) {
Base b1 = new Base();
Base b2 = new Sub();
}
}
class Base {
Base() {
method(100);
}
public void method(int i) {
System.out.println("base : " + i);
}
}
class Sub extends Base {
Sub() {
super.method(70);
}
public void method(int j) {
System.out.println("sub : " + j);
}
}
第4题
考核知识点:多态、重载、重写
public class Test04 {
public static void main(String[] args) {
A a1 = new A();
A a2 = new B();
B b = new B();
C c = new C();
D d = new D();
System.out.println("(1)" + a1.show(b));
System.out.println("(2)" + a2.show(d));
System.out.println("(3)" + b.show(c));
System.out.println("(4)" + b.show(d));
}
}
class A{
public String show(D obj){
return ("A and D");
}
public String show(A obj){
return "A and A";
}
}
class B extends A{
public String show(B obj){
return "B and B";
}
public String show(A obj){
return "B and A";
}
}
class C extends B{
}
class D extends B{
}
/*
* 1、分析方法列表和继承关系
* A类:
* public String show(D obj)
* public String show(A obj)
* B类:
* public String show(D obj)继承的
* public String show(A obj)重写
* public String show(B obj)自定义的
* C->B->A
* D->B->A
*
* 2、方法重载:找最合适的形参类型
* 3、方法重写:如果子类重写,就执行重写的
* 4、分析执行结果
* a1.show(b):a1没有多态引用,直接找A类的方法,b是B类对象,只能选择public String show(A obj) A and A
* a2.show(d):a2多态引用,执行子类的方法,d是D类对象,选最合适的public String show(D obj) A and D
* b.show(c):b没有多态引用,直接找B类的方法,c是C类的对象,选择最合适的public String show(B obj) B and B
* b.show(d):b没有多态引用,直接找B类的方法,d是D类对象,选最合适的public String show(D obj) A and D
*/
public class Test04 {
public static void main(String[] args) {
A a1 = new A();
A a2 = new B();
B b = new B();
C c = new C();
D d = new D();
System.out.println("(1)" + a1.show(b));
System.out.println("(2)" + a2.show(d));
System.out.println("(3)" + b.show(c));
System.out.println("(4)" + b.show(d));
}
}
class A {
public String show(D obj) {
return ("A and D");
}
public String show(A obj) {
return "A and A";
}
}
class B extends A {
public String show(B obj) {
return "B and B";
}
public String show(A obj) {
return "B and A";
}
}
class C extends B {
}
class D extends B {
}
第5题
考核知识点:多态、重载、重写
public class Test05 {
public static void main(String[] args) {
A a1 = new A();
A a2 = new B();
B b = new B();
C c = new C();
D d = new D();
System.out.println("(1)" + a1.show(b));
System.out.println("(2)" + a2.show(d));
System.out.println("(3)" + b.show(c));
System.out.println("(4)" + b.show(d));
}
}
class A {
public String show(C obj) {
return ("A and C");
}
public String show(A obj) {
return "A and A";
}
}
class B extends A {
public String show(B obj) {
return "B and B";
}
public String show(A obj) {
return "B and A";
}
}
class C extends B {
}
class D extends B {
}
package com.atguigu.test02;
/*
* 1、分析每个类的方法列表和继承关系
* A类:
* public String show(C obj)
* public String show(A obj)
* B类:
* public String show(C obj)继承的
* public String show(A obj)重写
* public String show(B obj)自定义的
* C->B->A
* D->B->A
*
* 2、方法重载:找最合适的形参类型
* 3、方法重写:如果子类重写,就执行重写的
* 4、如果特殊的重载,那么多态时,编译时先从父类中查找最合适的形参类型,然后如果子类如果有重写,执行子类重写的,如果没有重写,执行父类的。
* 5、分析执行结果
* a1.show(b):a1没有多态引用,直接找A类的方法,b是B类对象,只能选择public String show(A obj) A and A
* a2.show(d):a2多态引用,执行子类的方法,d是D类对象,但是因为此时编译时按A类编译,所以在编译期间先确定是调用
* public String show(A obj),而后执行子类重写的public String show(A obj) B and A
* 而不是直接选最合适的public String show(B obj)
* b.show(c):b没有多态引用,直接找B类的方法,c是C类的对象,选择最合适的public String show(C obj) A and C
* b.show(d):b没有多态引用,直接找B类的方法,d是D类对象,选最合适的public String show(B obj) B and B
*/
public class Test02 {
public static void main(String[] args) {
A a1 = new A();
A a2 = new B();
B b = new B();
C c = new C();
D d = new D();
System.out.println("(1)" + a1.show(b));
System.out.println("(2)" + a2.show(d));
System.out.println("(3)" + b.show(c));
System.out.println("(4)" + b.show(d));
}
}
class A {
public String show(C obj) {
return ("A and C");
}
public String show(A obj) {
return "A and A";
}
}
class B extends A {
public String show(B obj) {
return "B and B";
}
public String show(A obj) {
return "B and A";
}
}
class C extends B {
}
class D extends B {
}
第6题
考核知识点:属性与多态无关
public class Test06 {
public static void main(String[] args) {
Base b = new Sub();
System.out.println(b.x);
}
}
class Base{
int x = 1;
}
class Sub extends Base{
int x = 2;
}
package com.atguigu.test06;
/*
* 属性没有多态性,只看编译时类型
*/
public class Test06 {
public static void main(String[] args) {
Base b = new Sub();
System.out.println(b.x);
}
}
class Base{
int x = 1;
}
class Sub extends Base{
int x = 2;
}