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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· .NET Core 中如何实现缓存的预热?
· 三行代码完成国际化适配,妙~啊~
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?