目录

面向对象程序设计概念

对象的状态和行为

构造方法 

Java封装


面向对象程序设计概念

1.对象的抽象就是类,类的具体化就是对象。

2.类一般不包括存储数据的空间,而每个对象都有自己的数据空间。

3.面向对象程序设计把程序看作一系列的对象,通过对象间的交互解决问题。

举例:

Point class

state

int x,y

behavior

setLocation(x,y)

translate(int dx,int dy)

distance(Point p)

对象的状态和行为

我们先简单创建一个Point类:

public class Point{
    int x;
    int y;
}

保存的文件名应为Point.java

Java中,我们通常将x,y这类变量称为字段,一个对象的字段(或属性)是用于内部保存数据的变量,我们可以用基本的数据类型或其它对象来表示。一旦在类当中声明了这样的字段,这个类的所有对象都将拥有这样的变量,我们就可以用这个类来定义一些变量,比如type,name等。

使用Point类(示例):

Point p1 = new Point();
Point p2 = new Point();
System.out.println("the x-coord is" + p1.x); // 获取第一个点的横坐标
p2.y = 13;  // 给第二个点的横坐标赋值

实例方法举例:

public class Point{
    int x;
    int y;
    public void translate(int dx,int dy){
        x += dx;
        y += dy;
    }
}
Point p = new Point();
p.x = 7;
p.y = 2;
p.translate(-2,-1)

隐含参数:调用实例方法时引用的对象。(如上例中的p)

accessory:访问器,只访问对象的状态信息而不去修改,通常不带有参数

mutator:修改器,会修改对象的内部状态,在命名方法名称时多数以set开头

toString方法:用于返回一个字符串

public class Point{
    int x;
    int y;

    public void setLocation(int newX, int newY){
        x = newX;
        y = newY;
    }

    public void translate(int dx,int dy){
        setLocation(x + dx,y + dy)
    }

    public double distance(Point other){
        int dx = x - other.x;
        int dy = y - other.y;
        return Math.sqrt(dx * dx + dy * dy);
    }

    pubilc double distanceFromOrigin(){
        Point origion = new point();
        return distance(origin);
    }

    // 设计tostring方法用于打印点的坐标
    pubilc String toString{
        return "(" + x + "," + y + ")";
    }
}

构造方法 

public class Point{
    int x;
    int y;

    public void setLocation(int newX, int newY){
        x = newX;
        y = newY;
    }

    // 在给定的x/y位置构造一个点
    public point(int initialX, int initialY){
        x = initialX;
        y = initialY;
    }

    ......
}
public class Point{
    int x;
    int y;

    public void setLocation(int newX, int newY){
        x = newX;
        y = newY;
    }

    // 将点的位置设置为给定值
    public void setLocation(int newX, int newY){
        if (newX < 0 || newY < 0){
            throw new IllegalArgumentException();
        }
        x = newX;
        y = newY;
    }
}

this关键词:指类中的隐式参数(存储调用方法的对象的变量)

this.field()

this.method(parameters)

this(parameters)

public class Point{
    private int x;
    private int y;
    ...
    public void setLocation(int dx,int dy){
        this.x += dx;
        this.y += dy;
    }
}

构造不带参数的方法(设置默认值):

public Point(){
    x = 0;
    y = 0;
}

Java封装

在面向对象程式设计方法中,封装(英语:Encapsulation)是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法。

封装可以被认为是一个保护屏障,防止该类的代码和数据被外部类定义的代码随机访问。

要访问该类的代码和数据,必须通过严格的接口控制。

封装最主要的功能在于我们能修改自己的实现代码,而不用修改那些调用我们代码的程序片段。

适当的封装可以让程式码更容易理解与维护,也加强了程式码的安全性。

实现Java封装的步骤

1. 修改属性的可见性来限制对属性的访问(一般限制为private),例如:

public class Person { 
    private String name; 
    private int age; 
}

这段代码中,将 name 和 age 属性设置为私有的,只能本类才能访问,其他类都访问不了,如此就对信息进行了隐藏。

2. 对每个值属性提供对外的公共方法访问,也就是创建一对赋取值方法,用于对私有属性的访问,例如:

public class Person{ 
    private String name; 
    private int age; 
 
    public int getAge(){ 
        return age; 
    }  

    public String getName(){ 
        return name; 
    }  

    public void setAge(int age){ 
        this.age = age; 
    } 
 
    public void setName(String name){ 
        this.name = name; 
    } 
}

采用 this 关键字是为了解决实例变量(private String name)和局部变量(setName(String name)中的name变量)之间发生的同名的冲突。