栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

方法参数类型及返回值类型;链式编程

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

方法参数类型及返回值类型;链式编程

1、类名作为形式参数
基本类型作为参数传递:属于值传递,传递的是值,形参的改变,不影响实参
引用类型作为参数传递:属于引用传递,传递的是地址值,形参的改变会影响实参

当你以后看到一个方法的形参要一个‘ 类 ’类型,你就传递一个该类的对象

public class MyTest {
    public static void main(String[] args) {
        Student student = new Student();
        student.num=20;
//传递一个该类对象
        set(student);
        student.show(30);//30
        System.out.println(student.num);
    }
//形参需要一个‘类’类型
    public static void set(Student student){
        student.num=200;
    }
}


class Student{
    int num=10;
    public void show(int num){
        this.num=num;
    }
}
2、抽象类名作为形式参数

当你以后看到一个方法的形参要一个‘抽象类 ’类型,你就传递一个该抽象类的子类对象

public class MyTest1 {
    public static void main(String[] args) {
        B b = new B();
        //传递该抽象类的子类对象
        set(b);
        System.out.println(b.num);
    }
    //方法的形参要一个'抽象类'类型
    public static void set(A a){//多态 A a=b;
        //多态的形式访问成员变量,编译看左边,运行也看左边
        a.num=30;
    }
}

abstract class A{
    int num=10;
    public abstract void show(int a);
}

class B extends A{
    int num=20;
    @Override
    public void show(int a) {
        this.num=a;
    }
}
3、接口名作为形式参数

 当你以后看到一个方法的形参要一个‘接口’ 类型 ,你就传递一个该接口的子类对象

public class MyTest2 {
    public static void main(String[] args) {
        //当你以后看到一个方法的形参,要一个接口类型,你就传递一个接口的子类对象。
       C c = new C();
        test(c);
        c.show(50);
        System.out.println(c.num); //50
        System.out.println(Myinterface.num);//200
    }

    public static void test(Myinterface myInterface) { //MyInterface myInterface=b  //多态
        System.out.println(myInterface.num);//200
    }
}

interface Myinterface{
    //默认修饰符
    public static final int num = 200;
    public abstract void show(int a);
}
class C implements Myinterface{
    int num = 30;
    @Override
    public void show(int a) {
        this.num = a;
    }
}
4、类名作为返回值类型

当你以后看到一个方法的返回值类型,是一个类 类型,你就返回一个该类的对象

public class MyTest1 {
    public static void main(String[] args) {
        Student student = getStudent(20);
        student.num=30;
        Student s1 = student.show(40);
        System.out.println(student.num);
        System.out.println(s1.num);
    }
    //如果你以后看到一个方法的返回值类型,是一个'类 '类型,你就返回该类的对象。
    public static Student getStudent(int num){
        Student student = new Student();
        student.num=num;
        return student;
    }
}
class Student{
        int num=10;
        public Student show(int a){
            this.num=a;
     //this 代表一个该类的引用,哪个对象调用这个方法,方法中的this就代表谁
            return this;
        }
}
5、抽象类名作为返回值类型

当你以后看到一个方法的返回值类型是一个抽象类 类型,你就返回一个该抽象类的子类对象 

public class MyTest2 {
    public static void main(String[] args) {
        AA aa = test(120);  //多态AA aa=bb
        System.out.println(aa.num); //20
        aa.show();
    }
    //当你以后看到一个方法的返回值类型要一个 '抽象类 '类型,返回该类的子类对象
    public static AA test(int a) {
        BB bb = new BB();
        bb.num = a;
        return bb;
    }
}

abstract class AA {
    int num = 20;
    public abstract void show();
}

class BB extends AA {
    int num = 30;
    @Override
    public void show() {
        System.out.println(num); //120
    }
}
6、接口名作为返回值类型

当你以后看到一个方法的返回值类型是一个接口 类型,你就返回一个该接口的子类对象

public class MyTest {
    public static void main(String[] args) {
        MyInterface anInterface = getInterface(); // 多态MyInterface anInterface =aa
        System.out.println(anInterface.num);//20
        //向下转型
        AA aa= (AA) anInterface;
        System.out.println(aa.num);//90
    }
    //当你以后看到一个方法的,返回值类型是一个 接口类型,你就返回该接口的子类对象。
    public static MyInterface getInterface(){
        AA aa = new AA();
        aa.num=90;
        return aa;
    }
}

interface MyInterface{
   public static final int num=20;
}

class AA implements MyInterface{
    int num=100;
}
7、链式编程

当你调用完一个方法后,这个方法会返回一个对象,你就可以紧接着打点,继续调用该对象的方法

public class MyTest {
    public static void main(String[] args) {
        Student student = new Student();
        Student student1 = student.getStudent(new Student(), 20);
        int num1 = student1.getNum();
        System.out.println(num1); //20
        //链式编程:当你调用完一个方法后,这个方法会返回一个对象,你就可以紧接着打点,继续调用该对象的方法。
        int num = new Student().getStudent(new Student(), 20).getNum();
        System.out.println(num);//20
    }
}
class Student{
    int num=200;
    public Student getStudent(Student student,int a){
        Student s1 = new Student();
        s1.num=a;
        return s1;
    }
    public int getNum(){
        return this.num;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/602976.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号