package malnAV;
public class Work3_3 {
public static void main(String[] args) { //???
//方法 main 不能声明为“静态”;只能在静态类型或顶级类型中才能声明静态方法
Emp e1=new Emp(001,"张三");
Emp e2=new Emp(002,"李四");
Emp e3=new Emp(003,"王五");
Doc d1=new Doc(001,"会计");
e1.setMino(e3);
e2.setMino(e3);//e3为e1,e2的领导
e1.setDoc(d1);
e2.setDoc(d1);
e3.setDoc(d1);//e1,e2,e3同属d1部门
d1.setEmp(new Emp[]{e1,e2,e3});
System.out.println(e1.tell());
System.out.println(e2.tell());
System.out.println(e3.tell());
}
}
class Emp{//雇员
private int no ;
private String name;
private Emp mgr;
private Doc doc;
public Emp(){
}
public Emp(int no,String name){
this.no=no;
this.name=name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Emp getMino() {
return mgr;
}
public void setMino(Emp mino) {
this.mgr = mino;
}
public Doc getDoc() {
return doc;
}
public void setDoc(Doc doc) {
this.doc = doc;
}
public String tell(){
if(this.mgr.getName()!=null){
return "雇员编号:"+this.no+",姓名:"+this.name+",领导:"
+this.mgr.getName()+",部门:"+this.doc.getName();
}
else{
return "雇员编号:"+this.no+",姓名:"+this.name+",部门:"+this.doc.getName();
}
}
class Doc{//部门
private int no;
private String name;
private Emp []emp;
public Doc(){
}
public Doc(int no,String name){
this.no=no;
this.name=name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Emp[] getEmp() {
return emp;
}
public void setEmp(Emp[] emp) {
this.emp = emp;
}
public String tell(){
return this.no+"部门"+this.name+"部";
}
}
我说....哥们你到底是什么问题要说清楚啊,不然很少人会愿意帮你答的.....
方法 main 不能声明为“静态”;只能在静态类型或顶级类型中才能声明静态方法
一般 main 方法都为静态方法。
声明为静态方法,不需要该类的实例对象就可去调用,
若不为静态方法,需要创建类的对象才能调用。
你出现的Bug是什么!
程序有错误==》
19行--王五的领导--》显示空指针异常--取不到数据
Exception in thread "main" java.lang.NullPointerException
at malnAV.Emp.tell(Work3_3.java:59)
36行--缺少半个大括号