Java中传参和返回值方法总结

传参(参数传递)

类名作为形式参数

如果你以后看到一个方法的形参要一个“类”类型,就传一个该类对象

public class MyTest {
    public static void main(String[] args) {
        Student student = new Student();
        student.show(new Student(),20);
        System.out.println(student.num);
    }
}
class Student{
    int num= 10;
    public void show(Student stu, int num){
        stu.num=num;
    }
}

抽象类名作为形式参数

如果你以后看到一个方法的形参要一个“抽象类类型”,就传该类的子类对象

public class MyTest2 {
    public static void main(String[] args) {
        Dog dog = new Dog();
        setMethod(dog,114);
    }

    private static void setMethod(Animal an,int num) {
        an.show(num);
    }
}
abstract class Animal{
    int num=110;
    public abstract void show(int a);
}
class Dog extends Animal{
    int num=20;
    @Override
    public void show(int num) {
        System.out.println(num);
        System.out.println(this.num);
        System.out.println(super.num);
    }
}

接口名作为形式参数

如果你以后看到一个方法的形参要一个“接口”类型,你就穿一个接口的子类对象

public class MyTest3 {
    public static void main(String[] args) {
        MyClass myClass = new MyClass();
        test(myClass,22);
    }

    private static void test(Myinterface myinterface,int b) {
        myinterface.show(b);
    }
}
interface Myinterface{
    int NUM=100;
    void show(int a);
}
class MyClass implements Myinterface{
    int a = 10;
    @Override
    public void show(int a) {
        System.out.println(a);
        System.out.println(this.a);
        System.out.println(NUM);
        System.out.println(Myinterface.NUM);
    }
}

返回值

类名作为返回值类型

如果你看到一个方法的返回值类型,要一个“类”类型,你就返回一个该类对象

public class MyTest4 {
    public static void main(String[] args) {
        School school = new School();
        school.num=20;
        School school1 = school.getSchool(school, 111);
        System.out.println(school1.num);
        System.out.println(school==school1);
    }
}
class School{
    int num =10;
    public School getSchool(School school,int num){
        this.num=num;
        School school2 = new School();
        return this;
    }
}

接口名作为返回值类型

public class MyTest5 {
    public static void main(String[] args) {
        Myinterface1 anInterface = getInterface(10);
        anInterface.show(1000);
    }

    private static Myinterface1 getInterface(int num) {
        num=11;
        return new MyClass1();
    }
}
interface Myinterface1{
    void show(int num);
}
class MyClass1 implements Myinterface1{
    @Override
    public void show(int num) {
        System.out.println(num);
    }
}

抽象类名作为返回值类型

如果你以后看到一个方法的返回值类型,要一个“抽象类”类型,你返回一个该抽象类的子类对象

public class MyTest6 {
    public static void main(String[] args) {
        Animal1 animal = getAnimal(new Tiger());
        animal.test();
    }

    private static Animal1 getAnimal(Tiger tiger) {
        return tiger;
    }
}
abstract class Animal1{
    public abstract void test();
}
class Tiger extends Animal1{
    @Override
    public void test() {
        System.out.println("abc");
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值