Java basics (method parameters and method return values)

1. Method parameters

  • Class as method parameter: the object of this class is passed
  • Abstract class as a method parameter: the subclass object of the abstract class is passed
  • Interface as a method parameter: Passed is the implementation class object of the interface

Code example:

package day0526;
public class demo3 {
    public static void main(String[] args) {
         Student student=new Student();
         m1(student);
         Zi zi=new Zi();
         m2(zi);
         Ziinter ziinter=new Ziinter();
         m3(ziinter);
    }
    //普通类
    public static void m1(Student student){
        System.out.println(student.name);
    }
    //抽象类
    public static void m2(Abstractclass a){
        System.out.println(a.name);
    }
    //接口
    public static void m3(Inters inters){
        System.out.println(inters.name);
    }
}
class Student{
    String name="yyy";
}
abstract class Abstractclass{
    String name="wtc";
}
class Zi extends Abstractclass {
}
interface Inters{
    String name="yw";
}
class Ziinter implements Inters{

}

2. Return value

  • Ordinary class as a method return value: return the object of the class
  • Abstract class as a method return value: return the subclass object of the abstract class
  • Interface as a method return value: return the implementation class object of the interface

Code example:

package day0526;

public class demo4 {
    public static void main(String[] args) {
          Students a=m1();
        System.out.println(a.name);
    }
     //普通类
    public static Students m1(){
        Students s=new Students();
        return s;
    }
     //抽象类
    public static AbPerson m2(){
        AbPerson a=new PersonAb();
        return  a;
    }
    //接口
    public static Interfuction m3(){
        Interfuction i=new ZiImpl();
        return i;
    }
}
class Students{
    String name="yyy";
}
abstract  class  AbPerson{
    String name="wtc";
}
class PersonAb extends AbPerson{

}
interface Interfuction{
    String name="wy";
}
class ZiImpl implements Interfuction{

}

 

Published 75 original articles · praised 164 · 110,000 views

Guess you like

Origin blog.csdn.net/qq_41679818/article/details/90611080