Java类的定义

Java类的定义,第1张

package javalancs ;

/

Graphics objects for practical classes (Java 11 version)

@author Roger Garside/Richard Cardoe

@version Last Rewritten: 24/Sept/97

/

import javaawt ;

import javaawtevent ;

/

class to hold details about the shape to draw

/

class BasicShape

{

// name of the shape - RECTANGLE, OVAL, etc

int shape ;

// dimensions of the shape

int x, y, w, h ;

// colour of the shape

Color colour ;

// constructor to initialise the variables to default values

public BasicShape()

{

shape = -1 ;

x = -1 ;

y = -1 ;

w = -1 ;

h = -1 ;

colour = Colorgreen ;

} // end of constructor method

// constructor to initialise the variables to specifier values

public BasicShape(int sh, int x1, int y1, int w1, int h1, Color col)

{

shape = sh ;

x = x1 ;

y = y1 ;

w = w1 ;

h = h1 ;

colour = col ;

} // end of constructor method

} // end of class BasicShape

/

a canvas to draw on

/

class BasicCanvas extends Canvas

{

BasicGraphics parent ;

// constructor method

public BasicCanvas(BasicGraphics p)

{

parent = p ;

} // end of constructor method

// called when class is initialised to put window on the screen

// or when window needs to be redrawn

public void paint(Graphics g)

{

Dimension d = getSize() ;

int cx = dwidth / 2,

cy = dheight /2 ;

gsetColor(Colorblack) ;

gdrawRect(1, 1, dwidth - 3, dheight - 3) ;

int yy = 25 ;

while (yy < dheight)

{

if (yy % 100 == 0)

{

gdrawLine(1, yy, 11, yy) ;

gdrawLine(dwidth - 13, yy, dwidth - 3, yy) ;

}

else

{

gdrawLine(1, yy, 6, yy) ;

gdrawLine(dwidth - 8, yy, dwidth - 3, yy) ;

}

yy += 25 ;

}

int xx = 25 ;

while (xx < dwidth)

{

if (xx % 100 == 0)

{

gdrawLine(xx, 1, xx, 11) ;

gdrawLine(xx, dheight - 13, xx, dheight - 3) ;

}

else

{

gdrawLine(xx, 1, xx, 6) ;

gdrawLine(xx, dheight - 8, xx, dheight - 3) ;

}

xx += 25 ;

}

for (int i = 0 ; i < parentnoOfShapes ; i++)

{

gsetColor(parentshapeList[i]colour) ;

if (parentshapeList[i]shape == BasicGraphicsRECTANGLE)

{

gdrawRect(parentshapeList[i]x, parentshapeList[i]y,

parentshapeList[i]w, parentshapeList[i]h) ;

}

else if (parentshapeList[i]shape == BasicGraphicsFILLED_RECTANGLE)

{

gfillRect(parentshapeList[i]x, parentshapeList[i]y,

parentshapeList[i]w, parentshapeList[i]h) ;

}

else if (parentshapeList[i]shape == BasicGraphicsOVAL)

{

gdrawOval(parentshapeList[i]x, parentshapeList[i]y,

parentshapeList[i]w, parentshapeList[i]h) ;

}

else if (parentshapeList[i]shape == BasicGraphicsFILLED_OVAL)

{

gfillOval(parentshapeList[i]x, parentshapeList[i]y,

parentshapeList[i]w, parentshapeList[i]h) ;

}

else if ((parentshapeList[i]shape == BasicGraphicsTRIANGLE) ||

(parentshapeList[i]shape == BasicGraphicsFILLED_TRIANGLE))

{

int x1 = parentshapeList[i]x ;

int y1 = parentshapeList[i]y ;

int w1 = parentshapeList[i]w ;

int h1 = parentshapeList[i]h ;

Polygon p = new Polygon() ;

paddPoint(x1, y1 + h1) ;

paddPoint(x1 + w1, y1 + h1) ;

paddPoint(x1 + (w1 / 2), y1) ;

paddPoint(x1, y1 + h1) ;

if (parentshapeList[i]shape == BasicGraphicsTRIANGLE)

gdrawPolygon(p) ;

else

gfillPolygon(p) ;

}

}

} // end of method paint

} // end of class BasicCanvas

/

class to draw simple shapes in a window

/

public class BasicGraphics extends Frame implements ActionListener

{

// maximum width of window

private static final int MAX_WIDTH = 600 ;

// maximum height of window

private static final int MAX_HEIGHT = 400 ;

/

definition of a rectangle shape

/

public static final int RECTANGLE = 1 ;

/

definition of an oval shape

/

public static final int OVAL = 2 ;

/

definition of a triangle shape

/

public static final int TRIANGLE = 3 ;

/

definition of a filled-in rectangle

/

public static final int FILLED_RECTANGLE = 4 ;

/

definition of a filled-in oval

/

public static final int FILLED_OVAL = 5 ;

/

definition of a filled-in triangle

/

public static final int FILLED_TRIANGLE = 6 ;

BasicShape[] shapeList = new BasicShape[50];

int noOfShapes = 0;

private BasicShape newShape = new BasicShape();

private Button quit ;

/

constructor to lay out the window

/

public BasicGraphics()

{

setTitle("BasicGraphics Window") ;

setSize(MAX_WIDTH, MAX_HEIGHT + 50) ;

BasicCanvas c = new BasicCanvas(this) ;

add("Center", c) ;

Panel p = new Panel() ;

psetLayout(new FlowLayout()) ;

quit = new Button("Quit") ;

padd(quit) ;

quitaddActionListener(this) ;

add("South", p) ;

} // end of constructor method

/

handles button depression events, etc

/

public void actionPerformed(ActionEvent event)

{

dispose() ;

Systemexit(0) ;

} // end of method actionPerformed

/

set the type of shape that you want to draw

@param shape eg BasicGraphicsRECTANGLE

/

public void setShape(int shape)

{

if ((shape != RECTANGLE) && (shape != FILLED_RECTANGLE) &&

(shape != OVAL) && (shape != FILLED_OVAL) &&

(shape != TRIANGLE) && (shape != FILLED_TRIANGLE))

{

Systemerrprintln("This is not a valid shape");

Systemexit(1);

}

newShapeshape = shape ;

} // end of method setShape

/

set the dimensions of the shape that you want to draw

@param x x-coordinate of the top left hand corner of the bounding

rectangle

@param y y-coordinate of the top left hand corner of the bounding

rectangle

@param w width of the bounding rectangle

@param h height of the bounding rectangle

/

public void setDimensions(int x, int y, int w, int h)

{

if (newShapeshape == -1)

{

Systemerrprintln("You need to set the shape first");

Systemexit(1);

}

if ((x < 5) || (y < 5) || (w < 5) || (h < 5) ||

(x + w > MAX_WIDTH - 5) || (y + h > MAX_HEIGHT - 5))

{

Systemerrprintln("Invalid dimensions supplied") ;

Systemexit(1);

}

newShapex = x ;

newShapey = y ;

newShapew = w ;

newShapeh = h ;

} // end of method setDimensions

/

set the colour of the shape that you want to draw

@param colour the Color type (Colorred, Colorblue, etc)

/

public void setColour(Color colour)

{

if (newShapex == -1)

{

Systemerrprintln("You need to set the dimensions first");

Systemexit(1);

}

newShapecolour = colour ;

shapeList[noOfShapes] = new BasicShape(newShapeshape,

newShapex, newShapey,

newShapew, newShapeh,

newShapecolour) ;

noOfShapes++ ;

newShape = new BasicShape() ;

} // end of method setColour

/

draws the window on the screen with the specified shapes

/

public void draw()

{

setVisible(true) ;

} // end of method draw

} // end of class BasicGraphics

一个java文件中可以有很多类。不过注意以下几点:\x0d\1public 权限的类只能有一个(也可以一个都没有,但最多只有1个)\x0d\2这个java文件的文件名必须是public类的类名(一般的情况下,这里放置main方法是程序的入口。)\x0d\3若这个文件中没有public的类,则文件名随便是一个类的名字即可\x0d\4你用Javac 编译这个java文件的时候,它会给每一个类生成一个class文件\x0d\\x0d\你发的这个图能运行,因为有一个public类里面有main方法,这个main()方法是程序的入口\x0d\他这个程序的执行顺序是:进入main方法后,先NEW出来了一个ChildClass的对象,子类对象进入f()方法,执行对FatherClass中f()的调用,完了自己执行后面的方法体

实体

实体类是用于对必须存储的信息和相关行为建模的类。实体对象(实体类的实例)用于保存和更新一些现象的有关信息,例如:事件、人员或者一些现实生活中的对象。实体类通常都是永久性的,它们所具有的属性和关系是长期需要的,有时甚至在系统的整个生存期都需要。

一个实体对象通常不是某个用例实现所特有的;有时,一个实体对象甚至不专用于系统本身。其属性和关系的值通常由主角指定。执行系统内部任务时也可能要使用实体对象。实体对象的行为可以和其他对象构造型的行为一样复杂。但是,与其他对象不同的是,这种行为与实体对象所代表的现象具有很强的相关性。实体对象是独立于环境(主角)的。

实体对象代表了开发中的系统的核心概念。银行系统中实体类的典型示例是账户和客户。在一个网络处理系统中,典型的示例是节点和链接。

==

0-9

byte short int long float double char boolean

public

true false

江苏

int 0-4

属性, 方法

class new

final

接口

5

抽象 ,抽象

接口

2030

抽象

继承Thread ,实现Runnable

5

输入 , 输出

stop()

public static

init()

final

静态

catch  , finally

synchronized

与20相同

InputStream OutputStream Reader Writer

javasql

SeverSocket Socket

以上就是关于Java类的定义全部的内容,包括:Java类的定义、一个java文件可以定义好多类吗、java 实体类在程序的作用是什么等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/10114144.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-05
下一篇2023-05-05

发表评论

登录后才能评论

评论列表(0条)

    保存