博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java oop
阅读量:5958 次
发布时间:2019-06-19

本文共 4530 字,大约阅读时间需要 15 分钟。

本文主要作为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.566370614359172Radius is 2.0 Color is red Area is 12.566370614359172Radius is 1.0 Color is red Area is 3.141592653589793Circle with radius = 1.0 and color of redCircle 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; }

转载地址:http://cnexx.baihongyu.com/

你可能感兴趣的文章
利用WCF改进文件流传输的三种方式
查看>>
Spring学习总结(2)——Spring的常用注解
查看>>
关于IT行业人员吃的都是青春饭?[透彻讲解]
查看>>
钱到用时方恨少(随记)
查看>>
mybatis主键返回的实现
查看>>
org.openqa.selenium.StaleElementReferenceException
查看>>
数论之 莫比乌斯函数
查看>>
linux下查找某个文件位置的方法
查看>>
python之MySQL学习——数据操作
查看>>
Harmonic Number (II)
查看>>
长连接、短连接、长轮询和WebSocket
查看>>
day30 模拟ssh远程执行命令
查看>>
做错的题目——给Array附加属性
查看>>
Url.Action取消字符转义
查看>>
JQuery选择器大全
查看>>
HBase 笔记3
查看>>
java中通过绝对路径将图片存入数据库
查看>>
ConcurrentHashMap(Java8)源码分析
查看>>
Python文件处理之文件指针(四)
查看>>
Numpy用法详解
查看>>