1.1) Any variable created within these marks cannot be used outside of them.
When you create a variable or an object inside a method in one of your classes, it is usable only inside that method. The reason for this is the concept of variable scope.
访问权限:
public: 全访问
protected: package 内访问(default)
private: class 内访问
class 只能是public or default
局部变量只能用final修饰
static 不需要实例化就能访问,相当于全局变量
- 只能调用其他static方法
- 只能访问static数据
- 不能引用this or super
1.2) If more than one class is defined in the same source file, only one of the classes can be public. The other classes should not have public in their class statements. The name of the source code file must match the public class that it defines.
1.3) inner class : MainClass$InnerClass.class
a list of all the built-in methods that Java supports
http://download.oracle.com/javase/7/docs/api.
2) java.util.Vector
Vector<String> vector = new Vector<String>(300);
.add()
.get()
.contains()
.remove()
.sort()
Collections.sort()
Loop through a Vector: for (String str: vector){} : class StringLister
3) Point:import java.util.*;
public class StringLister {
String[] names = { "Spanky", "Alfalfa", "Buckwheat", "Daria",
"Stymie", "Marianne", "Scotty", "Tommy", "Chubby" };
public StringLister(String[] moreNames) {
Vector<String> list = new Vector<String>();
for (int i = 0; i < names.length; i++) {
list.add(names[i]);
}
for (int i = 0; i < moreNames.length; i++) {
list.add(moreNames[i]);
}
Collections.sort(list);
for (String name : list) {
System.out.println(name);
}
}
public static void main(String[] args) {
StringLister lister = new StringLister(args);
}
}
.move()
.translate()
import java.awt.*;
public class Point3D extends Point {
public int z;
public Point3D(int x, int y, int z) {
super(x,y);
this.z = z;
}
public void move(int x, int y, int z) {
super.move(x, y);
this.z = z;
}
public void translate(int x, int y, int z) {
super.translate(x, y);
this.z += z;
}
}
import java.awt.*;
class PointTester {
public static void main(String[] args) {
Point object1 = new Point(11,22);
Point3D object2 = new Point3D(7,6,64);
System.out.println("The 2D point is located at (" + object1.x
+ ", " + object1.y + ")");
System.out.println("\tIt’s being moved to (4, 13)");
object1.move(4,13);
System.out.println("The 2D point is now at (" + object1.x
+ ", " + object1.y + ")");
System.out.println("\tIt’s being moved -10 units on both the x "
+ "and y axes");
object1.translate(-10,-10);
System.out.println("The 2D point ends up at (" + object1.x
+ ", " + object1.y + ")\n");
System.out.println("The 3D point is located at (" + object2.x
+ ", " + object2.y + ", " + object2.z + ")");
System.out.println("\tIt’s being moved to (10, 22, 71)");
object2.move(10,22,71);
System.out.println("The 3D point is now at (" + object2.x
+ ", " + object2.y + ", " + object2.z + ")");
System.out.println("\tIt’s being moved -20 units on the x, y "
+ "and z axes");
object2.translate(-20,-20,-20);
System.out.println("The 3D point ends up at (" + object2.x
+ ", " + object2.y + ", " + object2.z + ")");
}
}
No comments:
Post a Comment