调用其他类成员的方法

z6883123 2018-03-16 03:49:30
public class A {
private String Authority = "sa";
B b = new B(Authority);
}

public class B {
public B b = new B();
public String Authority;
public B() {
;
}
public String getAuthority() {
return Authority;
}
public void setAuthority(String authority) {
Authority = authority;
}
public B(String Authority) {
b.setAuthority(Authority);;
}
}

public class C {
public C() {
}

public static void main(String[] args) {
C c = new C();
}
}

请问如何在C类中调用A类的b的函数?
...全文
915 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
捏造的信仰 2018-03-27
  • 打赏
  • 举报
回复
楼主要学会贴代码
public class Demo {
    public static void main(String[] args) {
        if (args.length == 0) {
            System.out.println("no args.");
        }
    }
}
像楼主的代码层级多,又没有排版,没法看。
Defonds 2018-03-26
  • 打赏
  • 举报
回复
先弄明白封装,以及 private、protected、public 的作用
  • 打赏
  • 举报
回复
public class A { private String Authority = "sa"; public B b = new B(Authority); } public class B { public B b = new B(); public String Authority; public B() { ; } public String getAuthority() { return Authority; } public void setAuthority(String authority) { Authority = authority; } public B(String Authority) { b.setAuthority(Authority);; } } public class C { public A a = new A(); public C() { } public static void main(String[] args) { C c = new C(); String test = c.a.b.getAuthority(); } }
  • 打赏
  • 举报
回复
你这代码自己写的吧 逻辑性不好 而且B类中没有有参构造函数,A类直接创建一个有参的B对象呢 在C方法中调用创建A对象 然后 对A的方法进行调用就好啦
kirawinder_159 2018-03-18
  • 打赏
  • 举报
回复
函数?方法吧? 有2个方法 1.c类持有B类对象 2.B类方法为静态方法

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧