博客
关于我
Java用类作为成员变量(LOL示例)
阅读量:568 次
发布时间:2019-03-09

本文共 1354 字,大约阅读时间需要 4 分钟。

Hero类:

package Demo14;public class Hero {       private String name;    private int age;    private Weapon weapon;    public Hero(String name, int age, Weapon weapon) {           this.name = name;        this.age = age;        this.weapon = weapon;    }    public String getName() {           return name;    }    public void setName(String name) {           this.name = name;    }    public int getAge() {           return age;    }    public void setAge(int age) {           this.age = age;    }    public Weapon getWeapon() {           return weapon;    }    public void setWeapon(Weapon weapon) {           this.weapon = weapon;    }    public Hero() {       }    public void Attack(){           System.out.println("年龄为"+ age +"的"+name+"用"+weapon.getCode()+"攻击敌人");    }}

Weapon类

package Demo14;public class Weapon {       private String code;    public String getCode() {           return code;    }    public void setCode(String code) {           this.code = code;    }    public Weapon() {       }    public Weapon(String code) {           this.code = code;    }}

Main方法

package Demo14;public class DemoMain {       public static void main(String[] args) {           Hero hero = new Hero();        hero.setName("盖伦");        hero.setAge(20);        Weapon weapon = new Weapon("多兰之剑");        hero.setWeapon(weapon);        hero.Attack();    }}

效果:

在这里插入图片描述

转载地址:http://njapz.baihongyu.com/

你可能感兴趣的文章
MySQL索引一篇带你彻底搞懂(一次讲清实现原理加优化实战,面试必问)
查看>>
MySQL索引下沉:提升查询性能的隐藏秘
查看>>
MySql索引为什么使用B+树
查看>>
MySQL索引为什么是B+树
查看>>
WARNING!VisualDDK wizard was unable to find any DDK/WDK installed on your system.
查看>>
MySQL索引介绍及百万数据SQL优化实践总结
查看>>
Mysql索引优化
查看>>
MySQl索引创建
查看>>
mysql索引创建及使用注意事项
查看>>
mysql索引创建和使用注意事项
查看>>
MySQL索引原理以及查询优化
查看>>
Mysql索引合并(index merge)导致的死锁问题
查看>>
MySQL索引和查询优化
查看>>
mysql索引底层数据结构和算法
查看>>
Mysql索引底层结构的分析
查看>>
MySQL索引底层:B+树详解
查看>>
Mysql索引总结
查看>>
mysql索引最左匹配原则理解以及常见的sql使用的索引情况的实测
查看>>
Mysql索引类型
查看>>
MySQL索引背后的数据结构及算法原理
查看>>