面向对象(五):形式参数和返回值的类型为类类型时的做法

本文详细解析了面向对象编程中,如何使用类、抽象类及接口作为方法的参数和返回值。通过实例展示了不同类型的对象如何在方法间传递,并讨论了返回对象的实践。

面向对象(五)

一、参数传递

1、类名作为形式参数传递

	如果一个方法的形参要一个类类型,那就传一个该类对象
举例:
public class MyTest {
    public static void main(String[] args) {
        //如果一个方法的形参要一个类类型,就传一个该类对象
        Student student = new Student();
        student.show(student,20);	//传入Student的一个对象(student)和一个整数值
        System.out.println(student.num);    //打印 20
    }
}
class Student{
    int num=100;
    public void show(Student stu,int num){
        stu.num=num;
    }
}

2、抽象类名作为形式参数

	如果一个方法的形参要一个抽象类 类型,那就传一个实现该类的子类的对象
举例:
public class MyTest2 {
    public static void main(String[] args) {
        //如果一个方法的形参要一个抽象类 类型,那就传一个该类的子类对象
        Dog dog = new Dog();
        setMethod(dog,4);   //传入实现Animal类的类的对象(dog)和一个整数值
    }
    private static void setMethod(Animal an,int num) {
        an.show(num);   //调用show()方法
    }
}
abstract class Animal{
    public abstract void show(int a);
}
class Dog extends Animal{
    @Override
    public void show(int num) {
        System.out.println(num);    //打印num值:4
    }
}

3、接口作为形式参数

	如果一个方法的形参要一个接口 类型,那就传一个实现该接口的子类的对象
举例:
public class MyTest3 {
    public static void main(String[] args) {
        //如果一个方法的形参要一个接口 类型,那就传一个实现该接口的子类的对象
        MyClass myClass = new MyClass();
        test(myClass,22);   //传入一个实现该接口的子类的对象(myClass)和一个整数值
    }

    private static void test(MyInterface myInterface,int b) {
        myInterface.show(b);    //调用show()方法
    }
}

interface MyInterface{
    void show(int a);
}

class MyClass implements MyInterface{
    @Override
    public void show(int a) {
        System.out.println(a);  //打印a的值:22
    }
}

二、返回值

1、类名作为返回值类型

	如果一个方法的返回值类型是类 类型,那就返回一个该类对象
举例:
public class MyTest {
    public static void main(String[] args) {
        //如果一个方法的返回值类型是一个类 类型,那就返回一个该类对象
        Student student = new Student();
        student.num=20;     //给student.num赋值20
        Student student1 = student.getStudent(111);   //获取返回值,即student对象,并赋给student1
        System.out.println(student1.num);   //打印student1.num的值:111
    }
}
class Student {
    int num=10;
    public Student getStudent(int num){
        this.num=num;   //相当于student.num=num;重新赋值111
        return this;    //谁调用这个方法this就代表谁,在此this代表student
    }
}

2、抽象类名作为返回值类型

	如果一个方法的返回值类型是接口类型,那就返回一个实现该接口的子类的对象
举例:
public class MyTest2 {
    //如果一个方法的返回值类型是接口类型,那就返回一个实现该接口的子类的对象
    public static void main(String[] args) {
        MyInterface anInterface = getInterface();   //调用getInterface()方法并接收其返回值,赋给anInterface
        anInterface.show(1000); //调用show()方法
    }
    
    private static MyInterface getInterface() {
        return new MyClass();   //返回一个实现该接口的子类的对象(new MyClass())
    }
}

interface  MyInterface{
    void show(int num);
}

class MyClass implements  MyInterface{
    @Override
    public void show(int num) {
        System.out.println(num);    //打印: 1000
    }
}

3、接口名作为返回值类型

	如果一个方法的返回值类型是抽象类 类型,那返回一个实现该抽象类的子类的对象
举例:
public class MyTest3 {
    //如果一个方法的返回值类型是抽象类 类型,那返回一个实现该抽象类的子类的对象
    public static void main(String[] args) {
        Animal animal = getAnimal(new Cat());   //调用getAnimal(Cat cat)方法并获取其返回值赋给animal
        animal.test();      //调用test()方法
    }
    private static Animal getAnimal(Cat cat) {
        return cat;     //返回实现抽象类Animal的子类的对象(cat)
    }
}

abstract class Animal{
    public abstract void test();
}

class Cat extends Animal{

    @Override
    public void test() {
        System.out.println("abc");  //打印abc
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值