Applet举例。源程序如下:import ; //声明applet使用了包中的Graphics类import ; //声明applet使用了包中的Applet类public extends Applet{ //声明一个名为MyApplet的公共类,它继承了Applet类public String s; //声明一个字符串public void init( ){ //Applet的初始化方法s = new String("Hello World !"); //创建一个字符串}public void paint(Graphics g){(s,60,40);}}一、解释:1、Graphics类使得Applet可以绘制直线、矩形、椭圆形、字符串等。此程序中,绘制了“Hello World!”字符串2、方法init( )实现了字符串的创建3、paint( )中,g为Graphics类的对象。它调用了Graphics的drawString方法绘制字符串。4、drawString方法中的第一个参数是要绘制的字符串s,后面两个参数(60,40)说明了字符串左下角所在的平面坐标,60为横坐标,40为纵坐标。5、Java的坐标系原点在左上角,纵轴正方向指向下方。坐标单位是像素。二、编译源程序为Class文件javac 三、Applet中没有main( )方法作为Java解释器的入口,因此必须编写HTML文件,把Applet嵌入HTML文件中,然后用appletviewer来运行,或在支持Java的浏览器上运行HTML文件。HTML文件内容如下:
My AppletJava provides all he luxuries of object-oriented programming:classhierarchy,inheritance,encapsulation,and polymorphism--in a context that is truly useful main reason for developing object-oriented software,besides clarity and simplicity,isthe desperate hope that somehow the objects you develop will be not onlyencourages software not only encourages software reuse,it demands writeany sort of Java program,no matter how simple,you must build on the classes and methods ofthe Java you have begun developing software in Java,you have two choices:● Build on the classes you have developed,thereby reusing them.● Rewrite your software from scratch,copying and tailoring useful parts of Java,the temptation to start from scratch is no longer 's object-orientedstructure forces you to decelop more useful,more tailorable, and much simpler software thefirst time Is Safer and More ReliableJava is safer to use than C++ because it keeps you from doing the things that you dobadly,while making it easier to do the things that you do won't automatically convert data have to explicitly convert from one the most undesirable conditions,will automatically convert one type has all the flexibility of assembly doesn''t assume that you know whatyou are makes sure that you pointers don'exist in can no longer access objects indirectly or by 't need declare objects and reference those objects pointerarithmetic is you need an indexed set of objects,you can use an array concept of "the address of an object" is eliminated from the programming errorsthat go another assembly language dinosaur is laid to a result,it becomes mucheasier to do things correctly on 's reliability extends beyond the language level to the compiler and the checks identify many programming errors that go undetected in otherprogamming checks go beyond syntactic checking to ensure that statements aresemantically checks are also more extendsive and your teacher or mom tellingyou to "Check your work twice to make sure it's right?"The Java linker understands classtypes and performs compiler-level type checking,adding redundancy to alsoperforms bounds checking and eliminates indirect object access,even under error conditions.译文为:JAVA提供了面向对象编程最有价值的一切:类的分级结构、继承、封装 和多态(动态绑定)——在真正意义上既可用又高效的(一切)。开发面向对象软件的主要原因,除简明性之外,是基于一种极端希望,那就是你开发出的对象将会被重用。JAVA不仅鼓励软件复用,更需要软件复用。编写任何JAVA软件,无论多简单,必须基于JAVA API提供的类和方法。一旦你开始用JAVA开发程序,你有两个选择:● 基于你已开发的类,即复用它们。● 基于对已有软件可用部分的挖补、复制、修改,重写你的软件。对于JAVA,从挖补开始已不再吸引人。JAVA的面向对象结构使你不得不从一开始就开发更好用,更易于修改而且更简单的软件。更安全且更可靠JAVA比C++使用起来更安全,因为它使你远离那些你容易弄糟的部分,同时使你擅长的部分变得更为容易。JAVA不会自动转换数据类型,你必须显式的将一个类型转换为另一个。C++在最不愿意看到的情况下,会自动将一个类型转换为另一个。它拥有汇编语言代码的所有灵活性。JAVA不会假设你知道你在做什么。它会确保你知道。C++指针在JAVA里不存在。你无法间接地或是随机地访问对象。(因为)你无需这样做。你声明对象并且直接访问它们,避免了复杂的指针运算。如果你需要一些编号的对象,你可以使用一个对象数组。“一个对象的地址”这一概念,就像另外一只“汇编语言恐龙”(注:恐龙代表过时但顽固的东西)一样,从编程错误中消失。所以,在JAVA里更容易把事情做对。JAVA的可靠性从语言级别一直延伸到编译器和运行系统级别。编译阶段可以检查出许多在别的语言(编译)中无法检查出的错误。这些检查不仅可以针对语法甚至可以保证语句在语义上的正确性。运行中的检查也更高效且更具扩展性。想想你的老师和母亲总对你说:“检查你的作业两次来保证你做对了。” JAVA连接器理解类的类型并且进行编译器级的类型检查,增加了可靠性的冗余(检查)。而且它还会进行边界检查,甚至在出错的情况下,也会消除 间接对象访问。