JavaTutorials之基础

  • Object

现实生活中对象有什么?
state:狗的颜色,体重等
behavior:狗叫
一个对象中可能还有另外的对象:自行车里还有轮子(内部类)。

软件模拟:
在这里插入图片描述
** Methods operate on an object’s internal state and serve as the primary mechanism for object-to-object communication. Hiding internal state and requiring all interaction to be performed through an object’s methods is known as data encapsulation — a fundamental principle of object-oriented programming.**

面向对象语言的最基本的法则:
隐藏内部的state,要求所有的Interaction由对象的方法来执行。
在这里插入图片描述
将state属性化,然后提供改变state的方法有以下好处:
模块化(Modularity):对象的属性和方法可以对立的脱离源码进行维护。
信息隐藏化(Information-hiding):和对象交互只能和对象向外提供的方法进行。
代码复用(Code re-use):项目里别人的对象你可以直接拿来用。
可插拔性和便于维护(Pluggability and debugging ease):如果这个对象有问题了,不用替换整个工程,仅仅替换掉这个对象就行。

  • Class

类是什么?现实生活中有好多自行车,每个自行车都是一个Object。这些自行车可能都是由同一个Blueprint制造出来的,所以这些对象有相同的component。在面向对象中我们说一个对象是一个类的Instance,一个实例。

class Bicycle {

    int cadence = 0;
    int speed = 0;
    int gear = 1;

    void changeCadence(int newValue) {
         cadence = newValue;
    }

    void changeGear(int newValue) {
         gear = newValue;
    }

    void speedUp(int increment) {
         speed = speed + increment;   
    }

    void applyBrakes(int decrement) {
         speed = speed - decrement;
    }

    void printStates() {
         System.out.println("cadence:" +
             cadence + " speed:" + 
             speed + " gear:" + gear);
    }
}

一个类Class的代码。
The fields cadence, speed, and gear represent the object’s state, and the methods (changeCadence, changeGear, speedUp etc.) define its interaction with the outside world.
我们发现这个类中不存在Main()方法:这是因为这个类不是一个完整的Application,它只是一个可能会用在Application中的自行车的抽象蓝图。The responsibility of creating and using new Bicycle objects belongs to some other class in your application.

class BicycleDemo {
    public static void main(String[] args) {

        // Create two different 
        // Bicycle objects
        Bicycle bike1 = new Bicycle();
        Bicycle bike2 = new Bicycle();

        // Invoke methods on 
        // those objects
        bike1.changeCadence(50);
        bike1.speedUp(10);
        bike1.changeGear(2);
        bike1.printStates();

        bike2.changeCadence(50);
        bike2.speedUp(10);
        bike2.changeGear(2);
        bike2.changeCadence(40);
        bike2.speedUp(10);
        bike2.changeGear(3);
        bike2.printStates();
    }
}
  • Inheritance

继承
一些对象和其他对象有一定程度的相同,山地车,公路车都有当前速度,当前脚踏板频率等,但是他们可能又有不同的地方。所以自行车是从山地车,公路车抽象出来的更高层的类别。Object-oriented programming allows classes to inherit commonly used state and behavior from other classes

In the Java programming language, each class is allowed to have one direct superclass, and each superclass has the potential for an unlimited number of subclasses:
在这里插入图片描述

在Java中一个Class只能由一个直接的Superclass,一个Superclass可能由无数个子Class。创建子类的语法如下:

class MountainBike extends Bicycle {

    // new fields and methods defining 
    // a mountain bike would go here

}

you must take care to properly document the state and behavior that each superclass defines, since that code will not appear in the source file of each subclass.
小心定义Superclass的State和Behavior,它们不会在子类的代码中出现。你要找出一些很抽象的很共有的东西。

  • Interface

对象通过他们向外界暴露的方法和外界交流,Method是对象和外界交流的接口(Interface)。

interface Bicycle {

    //  wheel revolutions per minute
    void changeCadence(int newValue);

    void changeGear(int newValue);

    void speedUp(int increment);

    void applyBrakes(int decrement);
}

你要去Implement(实现这些对外暴露的接口,也就是具体方法)

class ACMEBicycle implements Bicycle {

    int cadence = 0;
    int speed = 0;
    int gear = 1;

   // The compiler will now require that methods
   // changeCadence, changeGear, speedUp, and applyBrakes
   // all be implemented. Compilation will fail if those
   // methods are missing from this class.

    void changeCadence(int newValue) {
         cadence = newValue;
    }

    void changeGear(int newValue) {
         gear = newValue;
    }

    void speedUp(int increment) {
         speed = speed + increment;   
    }

    void applyBrakes(int decrement) {
         speed = speed - decrement;
    }

    void printStates() {
         System.out.println("cadence:" +
             cadence + " speed:" + 
             speed + " gear:" + gear);
    }
}

Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.

实现一个接口使得一个类对于它能向外界提供的行为更formal .Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler
如果你的一个类声明实现了某个接口,那么这个接口的代码要在类的源码中体现。

  • Package
    A package is a namespace that organizes a set of related classes and interfaces.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值