继承 多态 实例初始化 重写 等面试题练习

练习1

/*
 重载,重写,多态
 */
public class Exam1 {
	public static void main(String[] args) {
		A a1 = new A();//本态引用,只看A类
		A a2 = new B();//多态,看运行时类也就是B类
		B b = new B();//本态引用看B类,所以也是4个方法,A类和B类中的方法,A类中的方法有被重写的
		C c = new C();
		D d = new D();
		System.out.println("(1)" + a1.show(b));//多态参数,哪个能接受B类型的对象
		/*
		 * public String show(D obj){//D不行D是B的子类
			return ("A and D");
			}
			public String show(A obj){//A可以A是B的父类,所以结果是     A and A
				return "A and A";
			}
		 */
		System.out.println("(2)" + a2.show(d));
		/*
		  //这是父类的两个方法
		  public String show(D obj){   //这个直接符合D类所以结果就是    A and D
			return ("A and D");
			}
			
			//这个方法被子类重写了,也就是覆盖了,所以不运行了,直接看子类中的方法
//			public String show(A obj){
//				return "A and A";
//			}
			
			//这是子类的两个方法
			 public String show(B obj){
				return "B and B";
				}
			 public String show(A obj){
					return "B and A";
				}
		 */
		System.out.println("(3)" + b.show(c));
		/*
		 //这是父类的两个方法
		  public String show(D obj){
			return ("A and D");
			}
			
			//这个方法被子类重写了,也就是覆盖了,所以不运行了,直接看子类中的方法
//			public String show(A obj){
//				return "A and A";
//			}
			
			//这是子类的两个方法
			 public String show(B obj){ //没有直接的C类,按照就近原则,选择C的父类也就是B类 所以结果是  B and B
				return "B and B";
				}
			 public String show(A obj){
					return "B and A";
				}
		 */
		System.out.println("(4)" + b.show(d));
		/*
		 //这是父类的两个方法
		  public String show(D obj){ //直接就有D类了,是所以结果就是  A and D
			return ("A and D");
			}
			
			//这个方法被子类重写了,也就是覆盖了,所以不运行了,直接看子类中的方法
//			public String show(A obj){
//				return "A and A";
//			}
			
			//这是子类的两个方法
			 public String show(B obj){
				return "B and B";
				}
			 public String show(A obj){
					return "B and A";
				}
		 */
	}
}
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 and A
(2)A and D
(3)B and B
(4)A and D

*/

在这里插入图片描述

练习2

/*
 * 多态只针对方法
 * 属性没有多态,属性只看编译时类型
 */
public class Exam2 {
	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;
}

在这里插入图片描述

练习3

public class Father {
	String name = "atguigu";
	int age = 0;
}




/*
考权限修饰符
private在别的类中不能直接使用
 */
public class Child extends Father{
	public String grade;
	
	public static void main(String[] args) {      
		Father f = new Child();
		//System.out.println(f.name);//报错
	}
}

练习4

public class Person {
	public Person(){//构造器
		System.out.println("this is a Person.");//不继承
	}
}




/*
 * 考点:
 * (1)super()必须在构造器的首行
 * (2)静态方法中不能使用this
 */
public class Teacher extends Person {
	private String name = "tom";
	public Teacher(){//子类无参构造器
		System.out.println("this is a teacher.");
		//super();//空参 报错
	}
	public static void main(String[] args){
		Teacher tea = new Teacher();//本态
		//System.out.println(this.name);this is a teacher.Tom 报错
	}
}

在这里插入图片描述

练习5

/*
 * 
 * 考点:
 * (1)实例初始化过程
 * (2)多态
 * (3)super this
 * (4)重写
 */
public class Test {
	public static void main(String[] args) {
		Base b1 = new Base();//base:100   
		Base b2 = new Sub();//创建子类的对象
		/*
		 * (1)先执行父类的实例初始化
		 * 所以先执行这一句代码 method(100);  等价于this.method(100) 此时的this代表的是子类的对象,子类重写了method()
		 * 执行   System.out.println("sub : " + j);  结果是   sub : 100
		 * 
		 * (2)在执行子类的实例初始化
		 *   super.method(70);   前面有super所以执行的是父类的方法
		 *   System.out.println("base : " + i);    结果是:base : 70
		 */
	}
}
class Base{
	Base(){
		method(100);
	}//无参
	public void method(int i){
		System.out.println("base : " + i);
	}//有参
}

class Sub extends Base{
	
	Sub(){
		//****super();这一句虽然没有写,但是默认是存在的*****
		super.method(70);
	}//无参
	public void method(int j){
		System.out.println("sub : " + j);
	}//有参
}

在这里插入图片描述

练习6

class HelloA {
	public HelloA(){
		System.out.println("HelloA");
	}
	{
		System.out.println("I'm A Class");
	}
	static{
		System.out.println("static A");//静态
	}
}
public class HelloB extends HelloA{
	public HelloB(){
		System.out.println("HelloB");
	}
	{
		System.out.println("I'm B Class");
	}
	static{
		System.out.println("static B");//静态
	}

	public static void main(String[] args) {
		new HelloB();
	}

}

在这里插入图片描述

练习7

/*
 * (1)父类的实例化
 * 默认调用父类的无参构造	System.out.print("1");       1
 * (1)子类的实例化
 *  System.out.print("3");                       3
	father = new People(name +" F"); 显示创建父类的对象,显式调用父类有参构造    System.out.print("2"); 
 * 
 */
public class TestChild {
	public static void main(String[] args) {
		new Child("mike");
	}
}

class People{
	private String name;
	public People(){
		System.out.print("1");
	}
	public People(String name){
		System.out.print("2");
		this.name = name;
	}
}
class Child extends People{
	People father;
	
	public Child(String name){
		System.out.print("3");
		father = new People(name +" F");
	}
	public Child(){
		System.out.print("4");
	}
}

在这里插入图片描述

练习8

/*
 * 多态创建子类对象的过程
 * (1)导致父类的实例化初始化方法执行
 *    父类 x = 10;
 * 
 * this.print();    此时this代表子类的对象   执行 System.out.println("Son.x = " + x);  这个x是子类的x,还没有赋值,所以默认值0
                 父类 x = 20;
   (2)子类的实例化初始化方法的执行
   子类  x = 30
   this.print(); 此时的this也是子类 执行System.out.println("Son.x = " + x);   30
		x = 40;
(3)System.out.println(f.x);
这个是父类的X,为什呢,应为属性没有多态
 */
public class Exam3 {
	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);
	}
}

在这里插入图片描述

练习9

public class Father {
	public Father(){
		System.out.println("father create");
	}
}




public class Child extends Father{
	public Child(){
		System.out.println("child create");
	}
	
	public static void main(String[] args) {
		Father f = new Father();
		Child c = new Child();
	}
}

在这里插入图片描述

练习10

public class Father {
	private String name = "father";

	public String getName() {
		return name;
	}
}





/*
 * 因为子类没有重写getName(),离getName()声明最近,就近找到父类的this
 */
public class Test extends Father {
	
	private String name = "test";
	
	public static void main(String[] args) {
		Test test = new Test();
		System.out.println(test.getName());
	}
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值