java oop
本文主要作为java笔记,方便以后查看,大部分内容都源于以下网站:
http://www.ntu.edu.sg/home/ehchua/programming/index.html#Game
主要关于java面向对象编程。
Circle.java
public class Circle { public static final double RADIUS = 1.0; //final 表示常量,只读 public static final String COLOR = "red"; private double radius; private String color; //构造函数 constructor,无返回值 public Circle() { this.radius = RADIUS; this.color = COLOR; }
//constructor public Circle(double radius) { this.radius = radius; this.color = COLOR; }
//constructor public Circle(double radius, String color) { this.radius = radius; this.color = color; } //对private变量的获取和更改,通过下面的get和set函数来完成 // Public getters and setters for private variables public double getRadius() { return this.radius; } public void setRadius(double radius) { this.radius = radius; } public String getColor() { return this.color; } public void setColor(String color) { this.color = color; } // toString() to provide a short description of this instance public String toString() { return "Circle with radius = " + radius + " and color of " + color; } public double getArea() { return radius * radius * Math.PI; } }
主函数:
TestCircle.java
public class TestCircle { public static void main(String[] args) { Circle c1 = new Circle(2.0, "blue"); System.out.println("Radius is " + c1.getRadius() + " Color is " + c1.getColor() + " Area is " + c1.getArea()); Circle c2 = new Circle(2.0); System.out.println("Radius is " + c2.getRadius() + " Color is " + c2.getColor() + " Area is " + c2.getArea()); Circle c3 = new Circle(); System.out.println("Radius is " + c3.getRadius() + " Color is " + c3.getColor() + " Area is " + c3.getArea()); //invoke the toString() function System.out.println(c3); System.out.println(c3.toString()); } }
运行结果:
Radius is 2.0 Color is blue Area is 12.566370614359172 Radius is 2.0 Color is red Area is 12.566370614359172 Radius is 1.0 Color is red Area is 3.141592653589793 Circle with radius = 1.0 and color of red Circle with radius = 1.0 and color of red
另外一个例子:
MyPoint.java
public class MyPoint { private int x; private int y; public MyPoint() { x = 0; y = 0; } public MyPoint(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return x; } public void setY(int x) { this.y = y; } public void setXY(int x,int y) { this.x = x; this.y = y; } public String toString() { return "("+ x + ", " + y + ")"; } public double distance(int x, int y) { double xDiff = this.x - x; double yDiff = this.y - y; return Math.sqrt(xDiff*xDiff + yDiff*yDiff); } public double distance(MyPoint another) { double xDiff = this.x - another.x; double yDiff = this.y - another.y; return Math.sqrt(xDiff*xDiff + yDiff*yDiff); } }
TestMyPoint.java
public class TestMyPoint { public static void main(String[] args) { MyPoint p1 = new MyPoint(); System.out.println(p1); p1.setXY(0,3); System.out.println(p1); MyPoint p2 = new MyPoint(4,0); System.out.println(p2); System.out.println(p1.distance(4,0)); System.out.println(p1.distance(p2)); } }
运行结果:
(0, 0)
(0, 3)
(4, 0)
5.0
5.0
note:
Using the keyword "this", the constructor, getter and setter methods for a private variable called xxx of type T are as
follows:
public class Aaa {
// A private variable named xxx of type T
private T xxx;
// Constructor
public Aaa(T xxx) {
this.xxx = xxx;
}
// A getter for variable xxx of type T receives no argument and return a value of type T
public T getXxx() {
return xxx;
}
// A setter for variable xxx of type T receives a parameter of type T and return void
public void setXxx(T xxx) {
this.xxx = xxx;
}
}
For a boolean variable xxx, the getter shall be named isXxx(), instead of getXxx(), as follows:
// Private boolean variable
private boolean xxx;
// Getter
public boolean isXxx() {
return xxx;
}
// Setter
public void setXxx(boolean xxx) {
this.xxx = xxx;
}