Java-方法参数类型以及返回值类型

本文探讨Java中方法参数类型和返回值类型的问题,包括使用类名、抽象类名和接口名作为参数和返回值的情况。对于参数,传入对象需符合类型要求;对于返回值,会返回指定类型或其子类的对象。
摘要由CSDN通过智能技术生成

方法参数类型问题

类名作为形参参数

看到一个方法的形参要一个 类 类型,就传一个该类的对象

public class MyTest {
   
    public static void main(String[] args) {
   
              Student student = new Student();

        setStudent(student,100);

        student.show(new Student(),18);

        System.out.println(student.num); //18
    }

    private static void setStudent(Student student,int num) {
   
        student.num=num;
    }
}

class Student{
   
    int num=20;

    public void show(Student student,int num){
   
        this.num=num;
    }

抽象类名作为形式参数

看到一个方法的形参要一个抽象类 类型,就传一个该抽象类的 子类对象

public class MyTest2 {
   
    public static void main(String[] args) {
   
       

        MyClasss aa = new AA();

        setMyClss(aa, 200);
        System.out.println(aa.num); //10

    }

    priva
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值