这篇文章处于我对玩乐的兴趣。好吧。我以后还会有更多的此类文章的。
版权声明:不允许商业转载及引用。谢谢
详细文档的纳米盘地址:
FatSnake.jar
ToXinke.doc
ToXinke.docx
原程序.rar
Tuesday, June 23, 2009
Dear Everyone,
Thanks for choosing Java, the newest and most dynamic computer language in this world. It is easy and powerful, widely used in every corner of programming.
I am also a Java learner. I cannot teach you anything however – what you’ve known, I explain my meaning worse than our teacher.
But curriculums always focus on examinations. And I always focus on the practice. I can just help you to build a cool program which can be used, not only finishing the works, enjoying the pleasure of programming, and all of them are easy to learn, easy to handle, but powerful, extremely powerful.
OK. That’s all the stupid words I wanna say. Thank all of you again.
I wish everyone’s dream come true.
Yours faithfully,
K. M. Cauchy
各位好:
感谢选择Java,这个世界上最新和最有活力的语言。它很易学,同样,也很强大,因此,被广泛地应用于世界的每个角落。
本人也是一个Java的学习者。我当然比不上我们的老师,如果我来讲Java,肯定要比他更差。
不过课程永远面向考试,而我永远面向实践。我仅仅能够帮助大家建立一些很酷的程序,能用,但是不是难做。我希望大家能够享受制作程序的乐趣,而不是永远为了完成作业。
好了。我的废话到此结束了。再次感谢大家。
我衷心地祝愿每个人的梦想都能成真。
卡尔 麦泽尼斯 柯西
2009年6月23日
CONTENTS
1. Why is Java
2. 一条和你一样肥的蛇
Appendix附录:
对于什么是对象的解释(摘抄并翻译来自《Thinking in Java》)
Why is Java
很好。这个命题很重要。当前我们学习过的所有课程从来就没有系统的讨论过一个学习项目的意义。依我看来,学习项目的意义应当占到整个文章篇幅的50%左右。至于为什么,神经分析科学上世纪九十年代由著名的科学家解答过了(那个人的名字我也忘了,读高中的时候看过他的书):意义比兴趣会更加重要。换句话说,你会简单的因为意义学得更好。(看上去很荒谬,然而实际上是对的)。
而当然意义部分是不会考的。所以老师们向来会认为,意义方面不重要。或者说意义方面没有东西可以讲,“下去自己看吧”。
我不是老师,也绝不关心考试。所以我会长篇大论的讲讲意义。这一部分,我认为,绝对没有难度,你不用记住任何东西,也不用试着去掌握什么,一页一页的看吧。
我知道我们学过一点点VB吧。回忆一下VB的代码和C的代码有什么不同。比如我要声明一个字符串,VB里面是这样声明的:
Dim StingExample As String
而C呢?你的记忆里C是没有字符串这个概念的,只有字符。我们首先要先声明一个指针:
char * stringexample;
然后分配内存:
stringexample=(char *) malloc( /*一定的大小*/);
接着用指针引用内存来解决这个问题。用完了以后我们就可以释放了:
free(stringexample);
事实上我们目前所遇到的问题,都是比较简单的问题,有没有考虑过,如果有一天,malloc函数的返回值是0(也就是NULL),也就是说,系统没有内存给你呢?似乎是不用考虑的,然而,这种情况是有的,并且是经常会遇到的。你只要把malloc后面的东西改成65536就可以了(不信自己回去试一下)。这是什么概念呢?也就是说,如果你要一点内存来存放电子书,是有可能不够的。(以上的讨论够复杂吧)。
好的,上面的内容不看或者看不懂也没有关系。我举了这些例子,仅仅为了证明一点:VB比C要简单。这句话你必须要记住。
1998年的暑假,我写了我的第一个程序——一个简单的计算器程序。总共的代码不超过6行。
可以想象,VB是多么的简单。同样的程序用C来写,则至少要600行。
另外一个现象是,当时VB是一门很风靡的语言。
谁能够用最短的代码来写最强大的程序,哪一门语言就会获得最终的胜利。简单的语言总是最吃香的。这大概是为什么我们说Java可以胜利的原因吧。
图 TIOBE 2009年5月世界语言排行榜
(窃喜一下,上面我会的语言有:Java、C、C++、PHP、VB、C#、JavaScript、Perl、Delphi、SQL、Pascal、Matlab)
下面我来介绍一个简单的Java HelloWorld程序。事前先强调,这个部分重在明白意义,所以即使完全不了解也没有关系。我用的是Java 2 SE 1.6 Update 14。你如果没有的话,可以到http://java.sun.com/ 去下载。笔者将它安装到了D盘的j2me/jdk1.6.0_14目录下。
首先,你可以打开记事本,或者JCreator,来输入几行简单的程序:
Code: import java.awt.event.*; import javax.swing.*; import java.awt.*; public class Test1 extends JFrame{ Test1(){ this.setSize(300,200); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("Hello World!"); } public static void main(String args[]){ new Test1(); } } |
这里我保存在C盘根目录下面。
然后,单击“开始”->“运行”,键入“cmd”。然后打入以下命令:
path d:/j2me/jdk1.6.0_14/bin (笔者的jdk是安装在这个目录的)
cd ../../.. (总之cd到C盘的根目录就可以了)
c: (笔者的系统装在E盘,所以要多此一举)
javac test1.java (JavaC是编译程序,编译test1.java)
java Test1 (执行Java程序,注意Test1的大小写)
好了。这就是我们的程序运行的结果。出现了一个窗口。整体上面还算是简单吧。
那么我依旧给大家解释一下代码是什么意思。仍然说一句,即使你完全看不懂,没有关系。读下去,你只要明白意义就可以了。
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
这个没有什么意思。就好比C++里面引入#include一样。这两个单词在英文上的意思一模一样的。
public class Test1 extends JFrame{
}
这个是声明了一个类。在Java里,所有的东西都是用类来封装的。所谓的类,就是抽象来讲的对象。比如你声明了一个int对象,引用的时候就是一个对象这么回事。这个也不用去理解它。
Test1(){
this.setSize(300,200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Hello World!");
}
这个函数的意思是,对象的初始化,一个对象总要被初始化的。也就是它要被建立。setSize的意思是,设置这个窗体长300像素,宽200像素。setVisible是说,设置是否可见。setDefaultCloseOperation 是指退出选项(EXIT_ON_CLOSE顾名思义,就是关闭这个窗体,就退出程序)。下面那个函数就是设置窗体的标题为”Hello World”。
public static void main(String args[]){
new Test1();
}
这个函数,我想都不用说了吧,就是主函数。主函数的作用就是建立一个Test1类。
好了。说了那么多,你肯定会问我,这个Hello World的实例到底有什么用呢?这的确是我想重点说明的。可能各位没有写过什么有实际意义的程序,也不会太明白,事实上,当我第一次看到我做出来的窗体可以被双击打开的时候,我确实很激动。我在心里默念——那个是我做的。的确,你所看到的这一个窗体,和其他任何人所看到的任何窗体并没有本质的区别,无论是Word、魔兽、IE还是QQ,换言之,现在你可以说,这些程序你都可以写了。当然,你没有办法写全功能。总之,这与你以前写过的任何一个控制台程序有了本质的区别,它是一个可以用的程序,不再是为了完成作业场所迫不得已的累赘了。
程序的意义在于应用。这是我的观点。只有一个能够被使用的程序才是有价值的。否则,分文不值。
一条和你一样肥的蛇
如题,大家都知道我在说些什么。要完成一条贪食蛇,依照大家现在的水平,还有太多的东西需要补习。没有关系。看下去。接下来的内容绝对不是枯燥无味的说教,我仅仅会用一些最基本的概念。而不会涉及到极为复杂的内容,比如GUI。
好的。首先,第一个问题是,如何来描述一条蛇?
蛇具有蛇头。蛇具有蛇尾。蛇具有移动方向。蛇的每一个节点都可以链接成为一条完整的链表。
如果你觉得我上面的话仍然非常抽象的话,请思考,如果现在你手上有一条非常温顺的蛇,他的身体只会沿着它的头走过的路径移动的话。一条蛇的形状,是不是每一个你的手所触到的点的集合呢?是吧。你沿着蛇头往下摸,总可以确定这整条蛇具体的形状。
大家学过C中的结构;下面我用C来描述它:
struct snakenodes{
snakenodes *last;
snakenodes *next;
int x;
int y;
}
struct snake{
snakenodes *snakeHead;
snakenodes *snakeTail;
int length;
int HeadDirection;
}
这些浅显的英文对于大家来说应当是没有任何困难的了。这样我们就已经可以描述一条蛇了。
注意。这里我们使用的坐标x, y是相对于这条蛇的形状而言的。这与这条蛇具体的位置所在是区分开来的。
既然确定了这条蛇的形状,那么以下问题也变得很简单了:
1. 蛇是怎样运动的。这个问题很简单,蛇头向着蛇的前进方向移动,其他的节点移动到上一个节点的位置。(在我的程序里面用了一点小的技巧,那就是我是从尾部开始移动的)。
2. 怎么样吃东西。吞一个食物,蛇头沿着蛇的前进方向移动,增加一个节点在原来蛇头的位置,然后身体其他地方不动。
3. 如何判断蛇有没有撞到自己身体上面?很简单,如果蛇头的位置与蛇身体的任何一个点的位置相同,那么就撞上了。
下面来讨论蛇在具体的背景下的运动。现在既然这里有一条蛇,我们就把它看成是一个点,在整个游戏的坐标里面,蛇只有形状和位置这两个属性。既然上面我们已经确定了蛇的形状,那么,下面我们就只用确定蛇具体的位置。也就是说,我们只要设定了蛇的初始位置(偏移量),那么蛇自己用来确定形状的坐标,就可以用来确定蛇的具体位置:
比如:
蛇的移动。蛇每向着它蛇头的方向移动一个单位的长度,那么这条蛇蛇头的坐标就会有所变化。假设是向右的吧,那么这条蛇的蛇头的X坐标就会增大。也就是说,在蛇本身的移动中,就已经确定了蛇位置的这个因素。
那么,由于有了上面的解释,那么以下问题也可以得到解决:
1. 怎样判断蛇有没有出界?偏移量加上长度。如果出界了,那么游戏已经结束。
2. 给予干预。如果干预的方向与蛇前进的方向不相同且不相反,那么就更改蛇前进的方向。
好了。我的解释就这么多。我把源代码和可执行文件都放在了附件里。希望对大家的学习有帮助。
Appendix附录
事实上,很多很好的Java教科书上都有很详细的说明。比如Bruce Eckel的《Thinking in Java》。下面抄袭一下我的那本《Thinking in Java》(一时冲动买了本英文的,后悔啊!!):
Chapter 1: Introduction to Objects
“We cut nature up, organize it into concepts, and ascribe significances as we do, largely because we are parties to an agreement that holds throughout our speech community and is codified in the patterns of our language … we cannot talk at all except by subscribing to the organization and classification of data which the agreement decrees.”
Benjamin Lee Whorf (1897-1941)
The genesis of the computer revolution was in a machine. The genesis of our programming language thus tends to look like that machine.
But computers are not so much machines as they are mind amplification tools (“bicycles for the mind,” as Steve Jobs is fond of saying) and a different kind of expressive medium. As a result, the tools are beginning to look less like machines and more like parts of our minds, and also like other forms of expression such as writing, painting, sculpture, animation, and filmmaking. Object-oriented programming (OOP) is part of this movement toward using the computer as an expressive medium.
The progress of abstraction
All programming languages provide abstractions. It can be argued that the complexity of the problems you’re able to solve is directly related to the kind and quality of abstraction. By “kind” I mean, “What is it that you are abstracting?” Assembly language is a small abstraction of the underlying machine. Many so-called “imperative” languages that followed (such as FORTRAN, BASIC and C) were abstractions of assembly language. These languages are big improvements over assembly language, but their primary abstraction still required you to think in terms of the structure of the computer rather than the structure of the problem you are trying to solve. The programmer must establish the association between the machine model (in the “solution space,” which is the place where you’re modeling that problem, such as a computer) and the model of the problem that is actually being solved (in the “problem space”, which is the place where the problem exists). The effort required to perform this mapping, and the fact that it is extrinsic to the programming language, produces programs that are difficult to write and expensive to maintain, and as a side effect created the entire “programming methods” industry.
The alternative to modeling the machine is to model the problem you’re trying to solve. Early languages such as LISP and APL chose particular views of the world (“All problems are ultimately lists” or “All problems are algorithmic,” respectively). PROLOG casts all problems into chains of decisions. Languages have been created for constraint-based programming and for programming exclusively by manipulating graphical symbols. (The latter proved to be too restrictive.) Each of these approaches is a good solution to the particular class of problem they’re designed to solve, but when you step outside of that domain they become awkward.
The object-oriented approach goes a step further by providing tools for the programmer to represent elements in the problem space. This representation is general enough that the programmer is not constrained to any particular type of problem. We refer to the elements in the problem space and their representations in the solution space as “objects”. (You will also need other objects hat don’t have problem-space analogs.) The idea is that the program objects, so when you read the code of describing the solution, you’re reading words that also express the problem. This is a more flexible and powerful language abstraction than what we’ve had before. Thus, OOP allows you to describe the problem in terms of the problem, rather than in terms of the computer where the solution will run. There’s still a connection back to the computer: each object looks quite a bit like a little computer- it has a state, and it has operations that you can ask it to perform. However, this doesn’t seem like such a bad analogy to objects in the real world – they all have characteristics and behaviors.
Alan Kay summarized five basic characteristics of Smalltalk, the first successful object-oriented language and one of the languages upon which Java is based. These characteristics represent a pure approach to object-oriented programming:
1. Everything is an object. Think of an object as a fancy variable; it stored data, but you can “make requests” to that object, asking it to perform operations on itself. In theory, you can take any conceptual component in the problem you’re trying to solve (dogs, buildings, services, etc.) and represent it as an object in your program.
2. A program is a bunch of objects telling each other what to do by sending messages. To make a request of an object, you “send a message” to that object. More concretely, you can think of a message as request to call a method that belongs to a particular object.
3. Each object has its own memory made up of other objects. Put another way, you create a new kind of object by making a package containing existing objects. Thus, you can build complexity into a program while hiding it behind the simplicity of objects.
4. Every object has a type. Using the parlance, each object is an instance of a class, in which “class” is synonymous with “type.” The most important distinguishing characteristic of a class is “What message can you send to it?”
5. All objects of a particular type can receive the same messages. This is actually a loaded statement, as you will see later. Because an object of type “circle” is also an object of type “shape”, a circle is guaranteed to accept the shape messages. This means you can write code that talks to shapes and automatically handle anything that fits the description of a shape. This substitutability is one of the powerful concepts in OOP.
Booch offers an even more succinct description of an object:
An object has state, behavior and identity.
This means that an object can have internal data (which gives it state), methods (to produce behavior), and each object can be uniquely distinguished from every other object – to put this in a concrete sense, each object has a unique address in memory.
好了这本书的内容实在是太多,本来我还想把后面的接口和继承写下来的,但是觉得太长了,如果我一个人敲的话,绝对累死。所以我就截取到这里了。关于对象,实在是有太多的东西可以说,然而Bruce Eckel的话实在是说得经典。所以我就截取了他的话。希望大家看完以后能有一个大致的了解。