面向对象-专项练习

第一部分:this 练习

1.this指代成员变量

//第1题:
public class Test {
	private int a = 1;
	public void f1(int a) {
		System.out.println(a);
		System.out.println(this.a);
	}
	public static void main(String[] args) {
		Test t = new Test();
		t.f1(2); 
	}
}
//第2题:
public class Test {
	private int a = 1;
	public void f1(int a) {
		System.out.println(this.a);
		this.a = a;
	}
	public static void main(String[] args) {
		Test t = new Test();
		t.f1(2);
		System.out.println(t.a);
	}
}
//第3题:
public class Test {
	private int a = 1;
	public void f1(int b) {
		System.out.println(a); //问:这个a前面有this吗?
		System.out.println(b);
	}
}

2.this指代构造函数

public class Test {
	private int a = 1;
	public Test() {
		this(2);
	}
	public Test(int a) {
		this.a = a;
	}
	public static void main(String[] args) {
		Test t = new Test();
		System.out.println(t.a);
	}
}

3.this用于调用实例方法(一般不写)

public class Test {
	public void f1() {
		System.out.println("f1方法执行了...");
	}
	public void f2() {
		f1(); //问:f1方法前有this吗?
		System.out.println("f2方法执行了...");
	}
}

第二部分:super 练习

1.super用于访问父类的成员变量

public class Son extends Father{
	int a = 2;
	public void f1() {
		int a = 3;
		System.out.println(a);
		System.out.println(this.a);
		System.out.println(super.a);
	}
	public static void main(String[] args) {
		Son s = new Son();
		s.f1();
	}
}
class Father{
	int a = 1;
}

2.super用于调用父类的构造方法

public class Son extends Father{
	int a = 2;
	public Son() {
		super(); //如果不写呢?
		System.out.println("Son的无参构造执行了...");
	}
	public static void main(String[] args) {
		Son s = new Son();
	}
}
class Father{
	int a = 1;
	public Father() {
		System.out.println("Father的无参构造执行了...");
	}
}

3.super用于调用父类的实例方法

public class Son extends Father{
	int a = 2;
	public void f1() {
		super.f1();
	}
	public static void main(String[] args) {
		Son s = new Son();
		s.f1();
	}
}
class Father{
	int a = 1;
	public void f1() {
		System.out.println("Father的f1方法执行了...");
	}
}

第三部分:重载

在一个类中,方法名相同,参数列表不同的方法即为重载方法。

1.如下方法是重载方法吗?

public class MyTest {
    public void f1(){
    }
    public int f1(){
        return 1;
    }
}

2.如下方法是重载方法吗?

public class MyTest {
    public void f1(int a,int c){
    
    }
    public void f1(int a,int b){
        
    }
}

3.如下方法是重载方法吗?

public class MyTest {
    public void f1(int a,long b){

    }
    public void f1(long a,int b){

    }
}

第四部分:重写

在父子类中,因继承的父类方法无法满足子类的需求,就需要对方法进行重写。
重写规则:

1.访问权限必须大于等于父类
2.返回值类型:如果是基本类型或者void,重写时必须一致;如果是引用类型,子类重写时返回值类型要小于等于父类。
3.方法名和参数列表必须一致。
4.子类不能抛出比父类还大的异常。

1.如下方法是重写的方法吗?(练习:重写时,方法名和参数列表必须一致)

public class MyTest extends A{
    @Override
    public void f1() {  //有错吗?

    }
}
class A{
    public void f1(int a){

    }
}

2.如下方法是重写的方法吗?(练习:重写时,访问权限必须大于等于父类)

public class MyTest extends A{
    @Override
    void f1() {   //有错吗?

    }
}
class A{
    public void f1(){

    }
}

3.如下方法是重写的方法吗?(练习:返回值如果是基本类型或者void,重写时必须一致)

public class MyTest extends A{
    @Override
    public void f1() {  //有错吗?

    }
}
class A{
    public int f1(){
        return 1;
    }
}

4.如下方法是重写的方法吗?(练习:如果是方法返回值是引用类型,子类重写时返回值类型要小于等于父类的方法返回值类型)

public class MyTest extends A{
    @Override
    public A f1() {     //有错吗?
        return null;
    }
}
class A{
    public MyTest f1(){
        return null;
    }
}

5.如下方法是重写的方法吗?(练习:子类不能抛出比父类还大的异常)

public class MyTest extends A{
    @Override
    public void f1() throws IOException {  //有错吗?

    }
}
class A{
    public void f1() throws FileNotFoundException{

    }
}

6.看代码写结果

public class MyTest extends A{
    int a = 2;
    @Override
    public void f1(){
        System.out.println("MyTest的f1的方法");
    }
    public static void main(String[] args) {
        //1.运行结果是???
        MyTest myTest = new MyTest();
        System.out.println(myTest.a);
        myTest.f1();
        //2.运行结果是???
        A a = new A();
        System.out.println(a.a);
        a.f1();
        //3.运行结果是??? (向上造型)
        A a1 = new MyTest();
        System.out.println(a1.a);
        a1.f1();
    }
}
class A{
    int a = 1;
    public void f1(){
        System.out.println("A的f1的方法");
    }
}

第五部分:看代码写结果

第1题

public class Test extends Aoo{
	static int a = 1;
	int b;
	static {
		System.out.println("静态块走了...");
		a = 2;
	}
	public Test() {
		this(3);
		System.out.println("Test的无参构造走了...");
	}
	public Test(int b) {
		//super();
		this.b = b;
		System.out.println("Test的有参构造走了...");
	}
	public void f1() {
		System.out.println("f1方法走了...");
		System.out.println(a);
	}
	public static void main(String[] args) {
		Test t = new Test();
		t.f1();
		System.out.println(t.a);
		System.out.println(t.b);
	}
}
class Aoo{
	public Aoo(){
		System.out.println("Aoo的无参构造走了...");
	}
}

第2题

public class Null {
	public static void main(String[] args) {
		((Null)null).haha();
	}
	public static void haha() {
		System.out.println("haha...");
	}
}

第3题

public class Example {
    private int i = giveMeJ();
    private int j = 10;
    private int giveMeJ(){
        return j;
    }
    public static void main(String[] args) {
        Example example = new Example();
        System.out.println(example.i); //0
        System.out.println(example.j); //10
    }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值