鱼C论坛

 找回密码
 立即注册
查看: 1941|回复: 3

[已解决]关于组合的问题

[复制链接]
发表于 2022-3-11 08:42:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 ilsoviet1917 于 2022-3-11 08:54 编辑



为什么salary打印不出来?

  1. public class TestComponent {
  2.     public static void main(String[] args){
  3.         Employee e = new Employee("D", 2002, "m");
  4.         System.out.println(e.getSalary());
  5.         System.out.println(e.job.role);
  6.         e.job.setId(513);
  7.         System.out.println(e.job.getId());
  8.     }
  9. }

  10. class Job{
  11.     String role;
  12.     long salary;
  13.     int id;

  14.     public String getRole(){
  15.         return role;
  16.     }
  17.     public void setRole(String role){
  18.         this.role = role;
  19.     }

  20.     public long getSalary(){
  21.         return salary;
  22.     }

  23.     public void setSalary(long salary){
  24.         this.salary = salary;
  25.     }

  26.     public int getId(){
  27.         return id;
  28.     }

  29.     public void setId(int id) {
  30.         this.id = id;
  31.     }
  32. }

  33. class Employee{
  34.     Job job = new Job();//组合:一个类用另一个类作为成员
  35.     String sexual;

  36.     public String getSexual(){
  37.         return sexual;
  38.     }

  39.     public void setSexual(String sexual){
  40.         this.sexual = sexual;
  41.     }

  42.     public void setSalary(){job.setSalary(10000);}

  43.     public long getSalary(){
  44.         return job.salary;
  45.     }

  46.     public Employee(String role, int id, String sexual){
  47.         this.job.role = role;
  48.         System.out.println(role);
  49.         System.out.println(id);
  50.         System.out.println(sexual);
  51.     }
  52. }
复制代码
最佳答案
2022-3-11 08:59:57
本帖最后由 isdkz 于 2022-3-11 09:07 编辑
ilsoviet1917 发表于 2022-3-11 08:56
public void Person(){
        job.setSalary(10000);
    }


你只是定义了一个方法,你又没有去调用它,构造方法才会自动去调用,

在 Employee 类里面,Employee 才是它的构造方法,

普通的方法你要么在主方法里面去显式调用,要么在构造方法里面显式调用
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-3-11 08:48:39 | 显示全部楼层
本帖最后由 isdkz 于 2022-3-11 09:00 编辑

可以打印出来呀,只不过你还没设置初始值而已,所以默认初始值就是 0 了

  1. public class TestComponent {
  2.     public static void main(String[] args){
  3.         Employee e = new Employee("D", 2002, "m");
  4.         System.out.println(e.getSalary());
  5.         System.out.println(e.job.role);
  6.         e.job.setId(513);
  7.         System.out.println(e.job.getId());
  8.     }
  9. }

  10. class Job{
  11.     String role;
  12.     long salary;
  13.     int id;

  14.     public String getRole(){
  15.         return role;
  16.     }
  17.     public void setRole(String role){
  18.         this.role = role;
  19.     }

  20.     public long getSalary(){
  21.         return salary;
  22.     }

  23.     public void setSalary(long salary){
  24.         this.salary = salary;
  25.     }

  26.     public int getId(){
  27.         return id;
  28.     }

  29.     public void setId(int id) {
  30.         this.id = id;
  31.     }
  32. }

  33. class Employee{
  34.     Job job = new Job();//组合:一个类用另一个类作为成员
  35.     String sexual;

  36.     public String getSexual(){
  37.         return sexual;
  38.     }

  39.     public void setSexual(String sexual){
  40.         this.sexual = sexual;
  41.     }

  42.     public void setSalary(){job.setSalary(10000);}

  43.     public long getSalary(){
  44.         return job.salary;
  45.     }

  46.     public Employee(String role, int id, String sexual){
  47.         this.job.role = role;
  48.         this.setSalary();                                       // 注意这里
  49.         System.out.println(role);
  50.         System.out.println(id);
  51.         System.out.println(sexual);
  52.     }
  53. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-11 08:56:38 | 显示全部楼层
isdkz 发表于 2022-3-11 08:48
可以打印出来呀,只不过你还没设置初始值而已,所以默认初始值就是 0 了

  public void Person(){
        job.setSalary(10000);
    }

这里不算设置初始值了吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-11 08:59:57 | 显示全部楼层    本楼为最佳答案   
本帖最后由 isdkz 于 2022-3-11 09:07 编辑
ilsoviet1917 发表于 2022-3-11 08:56
public void Person(){
        job.setSalary(10000);
    }


你只是定义了一个方法,你又没有去调用它,构造方法才会自动去调用,

在 Employee 类里面,Employee 才是它的构造方法,

普通的方法你要么在主方法里面去显式调用,要么在构造方法里面显式调用
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-5 05:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表