Labels

Sunday, March 6, 2016

JAVA基础回顾 Part III:JAVA面向对象

Jinhai‘s JAVA cheat sheet

Codecademy学习笔记 Part III

类句法 class syntax

class Car {

    int modelYear;//instance variable

    public Car(int year) {//constructor

        modelYear = year;

    }
    public void startEngine() {//method without parameter or returned value
        System.out.println("Vroom!");
    }

    public void drive(int distanceInMiles) {//method with parameter but no returned value

        System.out.println("Miles driven: " + distanceInMiles);

    }
    public int numberOfTires() {//method without parameter but a returned value

        return 4;

    }

    public static void main(String[] args){//the main method

        Car myFastCar = new Car(2007);
        myFastCar.startEngine();
        myFastCar.drive(1628);


    }
}

JAVA面向对象总结

  • 类 Class: a blueprint for how a data structure should function
  • 构造函数 Constructor: instructs the class to set up the initial state of an object
  • 对象 Object: instance of a class that stores the state of a class
  • 方法 Method: set of instructions that can be called on an object
  • 参数 Parameter: values that can be specified when creating an object or calling a method
  • 返回值 Return value: specifies the data type that a method will return after it runs
  • 继承 Inheritance: allows one class to use functionality defined in another class

public static void main(String[] args ) 解释

  • public 表示可见度(visibility),它可以是 public, private, protected 或者 (如果省略的话) default.
  • static 是一个特殊的(可选的)关键字,表示接下来的方法可在不创建实例(instance)的情况下被调用。如果没有static的话,需要 实例化(instantiate)这个类,然后从这个类的对象(object)调用该方法。
  • void 表示该方法的返回值类型(return type), void表示该函数返回值为空。方法必须有返回值。
  • main( ... ) 表示该方法的名称。方法需要命名。具有参数也表明了它是一个方法
  • String[] args 是该方法的唯一参数(parameter)。 String[] 是该参数的类型, 表示字符串数组(array of Strings)。 args 是该参数的名字。参数需要命名。

No comments:

Post a Comment