小结
- class
- extends
- 一般private与public在class中的应用范围
- Naming a Method
- 类型结构 Class Constructors
- Passing Information to a Method or a Constructor
6.3 代码示例/小项目 CreateObjectDemo - class 与method结构体小结
1. class
基础格式如下,定义对象,声明及操作方法
class MyClass {
// field, constructor, and
// method declarations
}
2. extends
MyClass 是MySuperClass 的子类,并且仅是 implements the YourInterface interface.
class MyClass extends MySuperClass implements YourInterface {
// field, constructor, and
// method declarations
}
3. 一般private与public在class中的应用范围
3.1. 一般仅在class中用到的fields可以用私有类private,仅在这个class中可调用;需要外部调用的,可以间接的加一个public方法。
public class Bicycle {
//private fields
private int cadence;
private int gear;
private int speed;
//public methods
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}
public int getCadence() {
return cadence;
}
public void setCadence(int newValue) {
cadence = newValue;
}
public int getGear() {
return gear;
}
public void setGear(int newValue) {
gear = newValue;
}
public int getSpeed() {
return speed;
}
public void applyBrake(int decrement) {
speed -= decrement;
}
public void speedUp(int increment) {
speed += increment;
}
}
4.Naming a Method
一般是动词短语,首字母小写.
例如
run
runFast
getBackground
getFinalData
compareTo
setX
isEmpty
4.1 Overloading Methods
同一个class里可以有同名的method。
例如:
- 可以根据变量名不同命名不同method name: drawString, drawInteger, drawFloat, drawTwoFloat
- 也可以用同一个method name,然后让java自己根据变量判断用哪一个。(会混淆的不能这样用,例如变量类型和个数相同)
public class DataArtist {
...
public void draw(String s) {
...
}
public void draw(int i) {
...
}
public void draw(double f) {
...
}
public void draw(int i, double f) {
...
}
}
5. 类型结构 Class Constructors
- 给类型class提供结构,跟方法method不太一样。不同点是 a)使用class name 2)无return
- 其实就是给class 一个声明
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}
- 调用这个Class Constructors
Bicycle myBike = new Bicycle(30, 0, 8);
6. Passing Information to a Method or a Constructor
- 定义变量,在method中进行操作,最后又返回值。
//sudo code
public Polygon polygonFrom(Point[] corners) {
// method body goes here
return x;
}
public double computePayment(
double loanAmt,
double rate,
double futureValue,
int numPeriods) {
double interest = rate / 100.0;
double partial1 = Math.pow((1 + interest),
- numPeriods);
double denominator = (1 - partial1) / interest;
double answer = (-loanAmt / denominator)
- ((futureValue * partial1) / denominator);
return answer;
}
- 关于赋值与return
2.1. 第三行的赋值是给x分配了内存空间,内存空间中存入int 4.
2.2. 第四行调用passValue,return的值无处安放,丢了。
2.3. 第6行,所以需要把return 的值int 10存入x内存空间,这样x的值才会改变。
2.4. 第八行,也可以用作其他赋值,就方程method用。
class test2 {
public static void main(String[] args) {
int x = 4;
passValue(x);
System.out.println("first test x is " + x);
x = passValue(x);
System.out.println("second test x is "+ x);
int a1 = passValue(x);
System.out.println("another test of passing value is " + a1);
}
public static int passValue(int a){
a = 10;
return a;
}
}
- CreateObjectDemo
要新建一个项目,只放相关的包。不能想Python脚本语言那样逐个写。一般IDEA会检查和运行整个项目。
一看下去很复杂,其实拆分起来这三个class 主要是以下内容:
- 定义Point的结构,并且加constructor声明,调用和赋值
- 定义Rectangle的类,创建不同类型的constructor;创建相关method
- 在main()中调试。
//Point
public class Point {
public int x = 0;
public int y = 0;
// a constructor!
public Point(int a, int b) {
x = a;
y = b;
}
}
//Rectangle
public class Rectangle {
public int width = 0;
public int height = 0;
public Point origin;
// four constructors
public Rectangle() {
origin = new Point(0, 0);
}
public Rectangle(Point p) {
origin = p;
}
public Rectangle(int w, int h) {
origin = new Point(0, 0);
width = w;
height = h;
}
public Rectangle(Point p, int w, int h) {
origin = p;
width = w;
height = h;
}
// a method for moving the rectangle
public void move(int x, int y) {
origin.x = x;
origin.y = y;
}
// a method for computing the area of the rectangle
public int getArea() {
return width * height;
}
}
//main() function
public class CreateObjectDemo {
public static void main(String[] args) {
// Declare and create a point object and two rectangle objects.
Point originOne = new Point(23, 94);
Rectangle rectOne = new Rectangle(originOne, 100, 200);
Rectangle rectTwo = new Rectangle(50, 100);
// display rectOne's width, height, and area
System.out.println("Width of rectOne: " + rectOne.width);
System.out.println("Height of rectOne: " + rectOne.height);
System.out.println("Area of rectOne: " + rectOne.getArea());
// set rectTwo's position
rectTwo.origin = originOne;
// display rectTwo's position
System.out.println("X Position of rectTwo: " + rectTwo.origin.x);
System.out.println("Y Position of rectTwo: " + rectTwo.origin.y);
// move rectTwo and display its new position
rectTwo.move(40, 72);
System.out.println("X Position of rectTwo: " + rectTwo.origin.x);
System.out.println("Y Position of rectTwo: " + rectTwo.origin.y);
}
}
//Output
Width of rectOne: 100
Height of rectOne: 200
Area of rectOne: 20000
X Position of rectTwo: 23
Y Position of rectTwo: 94
X Position of rectTwo: 40
Y Position of rectTwo: 72
7. class 与method结构体小结
在class中定义是什么,可以怎么做;
然后在main()中进行操作。
以上小项目的结构是.
//project structure
project | class A
| class B
| main() method
|
//class A sudo code
class A {
//Class A structure
# what does A contains
// multiple constructors
# how does A construct?
# how to declare what A contains?
// multiple methods
# what can we do with class A objects?
}
2018.6.29 依旧是用java8官方文档入门