免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 884 | 回复: 0
打印 上一主题 下一主题

What Is a Class? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-06-18 16:13 |只看该作者 |倒序浏览
In the real world, you'll often find many individual objects all of the
same kind.
There may be thousands of other bicycles in existence, all of the same
make and model. Each bicycle was built from the same set of blueprints
and therefore contains the same components.
In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles.
A class is the blueprint from which individual objects are created.
The following
Bicycle
class is one possible implementation of a bicycle:
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);
       }
}
The syntax of the Java programming language will look new to you, but the design of this class is
based on the previous discussion of bicycle objects.
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.
You may have noticed that the Bicycle class does not contain a mainused in an application.
The responsibility of creating and using new Bicycle objects belongs to some other class in your application.
method.
That's because it's not a complete application; it's just the blueprint for bicycles that might be
Here's a
BicycleDemo
class that creates two separate Bicycle objects and invokes their methods:
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();
     }
}
The output of this test prints the ending pedal cadence, speed, and gear for the
two bicycles:
cadence:50 speed:10 gear:2
cadence:40 speed:20 gear:3
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/24141/showart_323740.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP