首页 > 其他 > 详细

引用数据类型作为方法的参数和返回

时间:2021-05-13 00:54:30      阅读:20      评论:0      收藏:0      [点我收藏+]

技术分享图片

 

 

import ch.qos.logback.classic.spi.STEUtil;

/**
 * @program: spring
 * @description: ${description}
 * @author: Mr.zw
 * @create: 2021-05-12 21:16
 **/
public class MyTest01 {
    public static void main(String[] args) {
        fun("abc");//方法的形参是String,传入的实参是String的对象
    }
    //String : 引用数据类型
    //"abc" ->String类的对象
    public static String fun(String s){
        //如果方法的返回值类型是String,方法中返回的是String的对象
        return "哈哈哈";
    }
}

  

/**
 * @program: spring
 * @description: ${description}
 * @author: Mr.zw
 * @create: 2021-05-12 21:24
 **/
//Student是一个类,类就是数据引用类型
public class Student {
}

  

import ch.qos.logback.classic.spi.STEUtil;

/**
 * @program: spring
 * @description: ${description}
 * @author: Mr.zw
 * @create: 2021-05-12 21:16
 **/
public class MyTest01 {
    public static void main(String[] args) {
        fun("abc");//方法的形参是String,传入的实参是String的对象
        Student s = new Student();
        fun2(s);
    }
    //String : 引用数据类型
    //"abc" ->String类的对象
    public static String fun(String s){
        //如果方法的返回值类型是String,方法中返回的是String的对象
        return "哈哈哈";
    }
    //方法的形参是Student,那么调用时的实参是Student的对象
    public static Student fun2(Student s){
        //如果方法的返回值是Student,方法中的返回值是Student的对象
        Student s2 = new Student();
        return s2;
    }
}

  引用数据类型作为成员变量

/**
 * @program: spring
 * @description: ${description}
 * @author: Mr.zw
 * @create: 2021-05-12 21:44
 **/
//英雄
public class Hero {
    //姓名
    private String name;
    //武器
    private Weapon weapon;
    //技能
    private Skill skill;

    public Hero(String name, Weapon weapon) {
        this.name = name;
        this.weapon = weapon;
    }

    public Hero() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Weapon getWeapon() {
        return weapon;
    }

    public void setWeapon(Weapon weapon) {
        this.weapon = weapon;
    }
    public void attack(){
        System.out.println(name+"使用了"+weapon.getName()+"造成了"+weapon.getPower()+"伤害");
    }
    public void release(Skill skill){
        skill.release();
    }
}

  

/**
 * @program: spring
 * @description: ${description}
 * @author: Mr.zw
 * @create: 2021-05-12 21:44
 **/
public class Weapon {
    private String name;
    private int power;
    private int price;

    public Weapon(String name, int power, int price) {
        this.name = name;
        this.power = power;
        this.price = price;
    }

    public Weapon() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPower() {
        return power;
    }

    public void setPower(int power) {
        this.power = power;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }
}

  

/**
 * @program: spring
 * @description: ${description}
 * @author: Mr.zw
 * @create: 2021-05-12 21:49
 **/
public class HeroTest {
    public static void main(String[] args) {
        Weapon weapon = new Weapon("暴风之剑",50,1300);
        Hero hero = new Hero("盖伦",weapon);
        hero.attack();

        hero.release(new Skill() {
            @Override
            public void release() {
                System.out.println("qwerdf");
            }
        });
    }
}

  

/**
 * @program: spring
 * @description: ${description}
 * @author: Mr.zw
 * @create: 2021-05-12 21:56
 **/
public interface Skill {
    void release();
}

  

引用数据类型作为方法的参数和返回

原文:https://www.cnblogs.com/demowei/p/14761943.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!