A class that uses another class is sometimes called a "client" of that class, i.e. Doglauncher is a client of dog.
也就是说使用一个method的class是这个method的“客户”,也称作client。
各有各的好处
The relative advantages of each approach will become clear as we gain additional practice throughout the course.
果然还是重新看一遍会有更好地感受,以为这样可以让我更清楚的想明白之前都讲了什么。
哦!明白了,果然还是要重新回过头来看这个讲义。
public class Doglauncher{
public static void main(String[] args) {
Dog d ;
d = new Dog();
d.weightInPounds = 20;
d.makeNoise();
}
}
这里的 Dog d 实际上就是create了一个新的object,课里面也解释过了:
- An Object in Java is an instance(例子,也可以说是实用,就是设一个新的variable那种感觉) of any class.
- The Dog class has its own variables, also known as instance variables or non-static variables. (这里说的就是weightInpounds吧?)
These must be declared inside the class, unlike languages like Python or Matlab, where new variables can be added at runtime. - The method that we created in the Dog class did not have the static keyword. We call such methods instance methods or non-static methods.
- To call the makeNoise method, we had to first instantiate (这里就是说如果我们想要call一个method,换句话说,用一个function,我们需要首先
建立这个method,用new这个词) - Then if we want to use this method, we called the new variable(d.makeNoise() instead of Dog.makeNoise())
- Once an object has been instantiated, it can be assigned to a declared variable of the appropriate type, e.g. d = new Dog() 这里的d就是declared variable。
Constructor in Java
As you've hopefully seen brefore, we usually construct objects in object oriented languages using a Constructor
(DogLauncher is a Constructor)
public static void() and Dog d = new Dog(20)
basically call this method and print something will be the Constructor.
Here, the instantiation is parameterized, saving
Instance method are actions that can be taken only by a specififc instance of a class. Static methods are actions that are taken by the class itself. Both are useful in different circumstances. As an example of a static medhos, the Math class provides a sqrt method. Because it is static, we can call it as follows:
x = Math.sqrt(100);
If sqrt had been an instance method, we would have instead the awkward syntax below. Luckily sqrt is a static method so we don't have to do this in real programs.
Math m = new Math();
x = m.sqrt(100);
这里实际上例子说的已经很清楚了,就是说如果是一个static method,你就一定要instantiation,不然的话就不能用,如果是一个instance method,那你就可以直接防数字进去了(直接call this method), 并不需要instiantiation。
Sometimes, it makes sense to have a class with both instance and static methods. For example, suppose want the ability to compare two dogs. One way to do this is to add a static method for comparing Dogs.
public static Dog maxDog(Dog d1, Dog d2) {
if (d1.weightInPounds > d2.weightInPounds) {
return d1;
}
return d2;
}
This method could be invoked by, for example:
Dog d = new Dog(15);
Dog d2 = new Dog(100);
Dog.maxDog(d,d2);
这里实际上就是说要举两个例子(造两个未知数d 和 d2)
Observe that we've invoked using the class name, since this method is a static method.
We could also have implemented maxDog as a non-static method, e.g.
public Dog maxDog(Dog d2) {
if (this.weightInPounds > d2.weightInPounds) {
return this;
}
return d2;
}
This method could be invoked by, for example:
Dog d = new Dog(15);
Dog d2 = new Dog(100);
d.maxDog(d2);
With what we've learned so far, it's time to
网友评论