java  实例的困惑
PHP中文网
PHP中文网 2017-04-17 17:24:16
0
3
183

请看如下两段代码:

第一段:

class Car
{
   private int curSpeed;
   private int maxSpeed;

   /** Create a new car object, but only if we are given the values we
    ** need to initialize our new object into a consistent  state.
    ** @param c Current speed of the car.
    ** @param m Maximum speed of the car.
    */
   public Car(int c, int m)
   {
      curSpeed = c;
      maxSpeed = m;
    }
  }
class CarDemo{
public static void main(String[],args){
Car a=new Car(2,10);
    }
}

第二段:

class Vehicle{
int passengers;
int speed;
}
class VehicleDemo{
public static void main(String[],args){
Vehicle a=new Vehicle();
a.speed=5;
}

第一段里我不懂public Car是什么,是方法吗?这不是实例啊,我知道可能是因为成员变量是private 使得在别的class里无法访问了,所以用public Car(int c,int m)在另一个demo类里来赋值?
第二段:Vehicle a=new Vehicle();这到底是实例(instance)还是对象(object)?

PHP中文网
PHP中文网

认证0级讲师

人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!