0

我有一个名为的超类Events和两个名为的子类,talk并且workshop.

在超类中它有一个实例变量maxNumberOfParticupants

maxNumberOfParticipants当我创建一些谈话对象和研讨会对象时,我想知道如何分享。

maxNumberOfParticpantsfortalk为 200,inmaxNumberOfParticipantsworkshop300;talk最大参与者数应该只能与talk对象共享,最大参与者数应该workshop只能与对象共享workshop

4

2 回答 2

1

1- 班级名称应为单数,首字母应为大写。(事件)

public class Event {

protected int maxNumberOfParticpants; // this level access is package and for childrens

public Event(int maxNumberOfParticipants){
this.maxNumberOfParticipants=maxNumberOfParticipants;
}

}

孩子们的

public class Talk extends Event {

public Talk(int maxNumberOfParticipants){
   super(maxNumberOfParticipants);
}


   public void someMethod(int max){
     if(this.maxNumberOfParticipants < max){
          // some code
     }
   }

}

public class Workshop extends Event{

public Workshop(int maxNumberOfParticipants){
   super(maxNumberOfParticipants);
}

}
于 2013-06-23T04:23:45.903 回答
0
class Event {
    protected int  maxNumberOfParticipants;
    public Event(int number) {
       this.maxNumberOfParticipants = number;
    }

    public int getMaxNumberPariticipants() {
       return maxNumberOfParticipants;
    }

}

class Talk extend Event {
    Talk(int number) {
       super(number)
     }
}


class Workshop extend Event {
    Workshop(int number) {
       super(number)
     }
}


public static void main(String a[]) {
    Event talk = new Talk(200);
    Workshop talk = new Workshop(300);
    System.out.println(talk.getMaxNumberPariticipants()) ---> 200
    System.out.println(workshop.getMaxNumberPariticipants()) ---> 300
}
于 2013-06-23T04:50:03.107 回答