Java面向对象基础—引用数据类型使用总结

本文详细介绍了Java中引用类型的应用,包括类名作为方法参数和返回值、抽象类和接口作为方法参数和返回值的情况,以及类名和抽象类、接口作为成员变量的实践。通过实例展示了如何在不同场景下正确使用这些引用类型。
摘要由CSDN通过智能技术生成

引用类型使用总结

1. 类名作为方法参数和返回值

public class Person {
    String name;// 姓名
    int age;// 年龄

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

public class Test {
    public static void main(String[] args) {
        // 引用数据类型作为方法的参数传递的是地址值
        // 类名作为方法参数
        // 创建Person类对象
        Person p = new Person("张三",18);
        // 调用method1方法,传入Person类对象
        method1(p);
        System.out.println(p.name+","+p.age);// 张三,19

    }

    // 定义一个方法,Person类作为方法的参数
    public static void method1(Person p){
        p.age = 19;
    }
}

public class Test2 {
    public static void main(String[] args) {
        // 引用数据类型作为方法的返回值返回的是地址值
        // 类名作为方法返回值
        Person p1 = method2();
        System.out.println(p1.name+","+p1.age);// 李四,18
    }

    // 定义一个方法,Person类作为方法的返回值类型
    public static Person method2(){
        Person p = new Person("李四",18);
        return p;
    }

}

2. 抽象类作为方法参数和返回值

  • 抽象类作为形参:表示可以接收任何此抽象类的"子类对象"作为实参;
  • 抽象类作为返回值:表示"此方法可以返回此抽象类的任何子类对象";
public abstract class Animal {
    public abstract void eat();
}

public class Dog extends Animal {
    @Override
    public void eat() {
        System.out.println("狗吃骨头...");
    }
}

public class Test {
    public static void main(String[] args) {
        // 结论: 方法的参数为抽象类,那么就需要传入该抽象类的子类对象
        // 结论: 方法的返回值类型为抽象类,那么就需要返回该抽象类的子类对象

        // 调用method方法,需要传入的是Animal类子类对象
        Dog dog = new Dog();
        method(dog);

        method(new Animal() {
            @Override
            public void eat() {
                System.out.println("吃...");
            }
        });
    }

    // 抽象类作为方法的参数
    public static void method(Animal anl){
        anl.eat();
    }

    // 抽象类作为方法的返回值
    public static Animal method2(){
        //return new Dog();
        return new Animal() {
            @Override
            public void eat() {
                System.out.println("吃...");
            }
        };
    }
}

3. 接口作为方法参数和返回值

  • 接口作为方法的形参:【同抽象类】
  • 接口作为方法的返回值:【同抽象类】
public interface IA {
    void show();
}

public class Test {
    public static void main(String[] args) {
        // 结论: 接口作为方法的参数,那么必须传入该接口的实现类对象
        // 结论: 接口作为方法的返回值,那么必须返回该接口的实现类对象
		
        // 传入接口的实现类对象
        method1(new IA() {
            @Override
            public void show() {
                System.out.println("show...");
            }
        });
    }

    // 接口作为方法的参数
    public static void method1(IA ia){

    }

    // 接口作为方法的返回值
    public static IA method2(){
        // 返回接口的实现类对象
        return new IA() {
            @Override
            public void show() {

            }
        };
    }
}

4. 类名作为成员变量

public class Person {
    // 姓名
    String name;// String 类 jdk提供
    // 身份证
    IdCard idCard;
}

public class IdCard {// 身份证
    String idNum;
}
public class Test {
    public static void main(String[] args) {
        // 创建Person对象
        Person p = new Person();
        // 创建身份证对象
        IdCard id = new IdCard();
        // 给身份证对象赋值一个身份证号码
        id.idNum = "401234567890";

        // 给p对象的name赋值一个名字
        p.name = "张三";
        // 给p对象的身份证赋值一个身份证对象
        p.idCard = id;

        // 需求: 取出p对象的身份证号码
        System.out.println(p.idCard.idNum);// 401234567890
    }
}

5. 抽象类作为成员变量

  • 抽象类作为成员变量——为此成员变量赋值时,可以是任何它的子类对象
public abstract class Animal {
}
public class Dog extends Animal {
}
public class Person {
    Animal anl;// 抽象类作为成员变量
}

public class Test {
    public static void main(String[] args) {
        // 结论: 抽象类作为成员变量,在赋值的时候只能赋该抽象类的子类对象
        Person p = new Person();
        p.anl = new Dog();
    }
}

6. 接口作为成员变量

  • 接口类型作为成员变量——【同抽象类】
interface Animal{
   
}

class Dog implements Animal{

}
class Person{
   Animal anl;
}
public class Test {
   public static void main(String[] args) {
       // 结论: 接口作为成员变量,赋值的时候需要赋该接口的实现类对象
       Person p = new Person();
       p.anl =  new Dog();
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值