1.三角形类:
import java.awt.*;
import java.applet.*;
class rectangles //定义 rectangles类
{
public int Width;
public int Height;
public rectangles(int CWidth, int CHeight) //构造方法
{
Width=CWidth;
Height=CHeight;
}//构造方法定义毕
public void Showrectangle(Graphics g,int x,int y)
{
g.drawString("Width:"+Width,x,y);
g.drawString("Height:"+Height,x,y+20);
g.drawString("Area:"+Height*Width,x,y+40);
}
} // dogs类定义毕
public class c4_1 extends Applet
{
public void paint(Graphics g)
{
rectangles myrect=new rectangles(20,30);
myrect.Showrectangle(g,25,120);
}
}
//html文件:
<html><head><title>applet program</title></head><body><applet code="a.class"
Width=200 height=250></applet></body></html>
2.学生信息
import java.awt.*;
import java.applet. *;
class students //定义students类
{
//以下三行定义students类的数据成员
public String stu_NO ;
public String stu_name;
public int grade1;
public int grade2;
//以下六行是students类的成员方法Showstudent( )的定义
public void Showstudent(Graphics g,int x,int y)
{
g.drawString("stu_N.O:"+stu_NO,x,y);
g.drawString("stu_name:"+stu_name,x,y+20);
g.drawString("totalgrade:"+(grade1+grade2),x,y+40);
}
//成员方法Showstudent( )定义完成
}// students类定义毕
public class c4_2 extends Applet
{
public void paint(Graphics g)
{
//以下为创建对象
students liuxing; //声明了一个名为liuxing的对象,它属于students类
liuxing=new students( ); //建立lixing对象,为它分配存储空间
students hanfeng=new students( );//声明hanfeng对象的同时建立该对象
students yingwei=new students( );//声明yingwei对象的同时建立该对象
students zhaoke=new students( );//声明zhaoke对象的同时建立该对象
students lishu=new students( );//声明lishu对象的同时建立该对象
//以下几行引用对象的数据成员,将一组值赋给对象的数据成员
liuxing.stu_name="liuxing";
liuxing.stu_NO="10010";
liuxing.grade1=95;
liuxing.grade2=94;
hanfeng.stu_name="hanfeng";
hanfeng.stu_NO="10011";
hanfeng.grade1=85;
hanfeng.grade2=92;
yingwei.stu_name="yingwei";
yingwei.stu_NO="10012";
yingwei.grade1=68;
yingwei.grade2=84;
zhaoke.stu_name="zhaoke";
zhaoke.stu_NO="10013";
zhaoke.grade1=75;
zhaoke.grade2=77;
lishu.stu_name="lishu";
lishu.stu_NO="10014";
lishu.grade1=89;
lishu.grade2=91;
//以下两行引用对象的成员方法
liuxing.Showstudent(g,25,25);
hanfeng.Showstudent(g,200,25);
yingwei.Showstudent(g,25,120);
zhaoke.Showstudent(g,200,120);
lishu.Showstudent(g,25,240); } }
3.用构造方法写:
import java.awt.*;
import java.applet.*;
class students //定义 students类
{
public String stu_NO ;
public String stu_name;
public int grade1;
public int grade2;
public students(String Cstu_NO,String Cstu_name,int Cgrade1,int Cgrade2) //构造方法
{
stu_NO=Cstu_NO;
stu_name=Cstu_name;
grade1=Cgrade1;
grade2=Cgrade2;
}//构造方法定义毕
public void Showstudent(Graphics g,int x,int y)
{
g.drawString("stu_NO:"+stu_NO,x,y);
g.drawString("stu_name:"+stu_name,x,y+20);
g.drawString("totalgrade:"+(grade1+grade2),x,y+40);
}
} // dogs类定义毕
public class c4_3 extends Applet
{
public void paint(Graphics g)
{
students liuxing=new students("10010","liuxing",95,94);
students hanfeng=new students("10011","handeng",85,92);
students yingwei=new students("10012","yingwei",68,84);
students zhaoke=new students("10013","zhaoke",75,77);
students lishu=new students("10014","lishu",89,91);
liuxing.Showstudent(g,25,25);
hanfeng.Showstudent(g,200,25);
yingwei.Showstudent(g,25,120);
zhaoke.Showstudent(g,200,120);
lishu.Showstudent(g,25,240);
}
}
4. 用pulic类来定义一个无修饰符的static,final类!
import java.awt.*;
import java.applet.*;
class pp{
static int n=33;
final int nn; //声明nn,但没有赋初值
final int k=55; //声明k并赋初值40
pp( )
{ nn= ++n; }//在构造方法中给nn赋值
}
public class c4_4 extends Applet
{
public void paint(Graphics g)
{
pp m1=new pp( ); //创建对象m1,使其静态数据成员nn的值为21
pp m2=new pp( ); //创建对象m2,使其静态数据成员nn的值为22
// m1.nn=90; //这是一个错误的赋值语句,因为nn是标识符常量
g.drawString("m2.nn="+m2.nn,20,30);
g.drawString("m2.k="+m2.k,20,50);
g.drawString("m1.nn="+m1.nn,20,70);
g.drawString("m1.k="+m1.k,20,90);
g.drawString("m1.nn="+m1.nn,20,110);g.drawString("m2.k="+m2.k,20,130);