当前位置:首页 » 编程语言 » java类定义

java类定义

发布时间: 2022-01-10 20:00:15

java类的定义

package java.lancs ;

/**
* Graphics objects for practical classes (Java 1.1 version)
* @author Roger Garside/Richard Cardoe
* @version Last Rewritten: 24/Sept/97
*/

import java.awt.* ;
import java.awt.event.* ;

/*
* 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 = Color.green ;
} // 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 = d.width / 2,
cy = d.height /2 ;
g.setColor(Color.black) ;
g.drawRect(1, 1, d.width - 3, d.height - 3) ;
int yy = 25 ;
while (yy < d.height)
{
if (yy % 100 == 0)
{
g.drawLine(1, yy, 11, yy) ;
g.drawLine(d.width - 13, yy, d.width - 3, yy) ;
}
else
{
g.drawLine(1, yy, 6, yy) ;
g.drawLine(d.width - 8, yy, d.width - 3, yy) ;
}
yy += 25 ;
}
int xx = 25 ;
while (xx < d.width)
{
if (xx % 100 == 0)
{
g.drawLine(xx, 1, xx, 11) ;
g.drawLine(xx, d.height - 13, xx, d.height - 3) ;
}
else
{
g.drawLine(xx, 1, xx, 6) ;
g.drawLine(xx, d.height - 8, xx, d.height - 3) ;
}
xx += 25 ;
}

for (int i = 0 ; i < parent.noOfShapes ; i++)
{
g.setColor(parent.shapeList[i].colour) ;

if (parent.shapeList[i].shape == BasicGraphics.RECTANGLE)
{
g.drawRect(parent.shapeList[i].x, parent.shapeList[i].y,
parent.shapeList[i].w, parent.shapeList[i].h) ;
}
else if (parent.shapeList[i].shape == BasicGraphics.FILLED_RECTANGLE)
{
g.fillRect(parent.shapeList[i].x, parent.shapeList[i].y,
parent.shapeList[i].w, parent.shapeList[i].h) ;
}
else if (parent.shapeList[i].shape == BasicGraphics.OVAL)
{
g.drawOval(parent.shapeList[i].x, parent.shapeList[i].y,
parent.shapeList[i].w, parent.shapeList[i].h) ;
}
else if (parent.shapeList[i].shape == BasicGraphics.FILLED_OVAL)
{
g.fillOval(parent.shapeList[i].x, parent.shapeList[i].y,
parent.shapeList[i].w, parent.shapeList[i].h) ;
}
else if ((parent.shapeList[i].shape == BasicGraphics.TRIANGLE) ||
(parent.shapeList[i].shape == BasicGraphics.FILLED_TRIANGLE))
{
int x1 = parent.shapeList[i].x ;
int y1 = parent.shapeList[i].y ;
int w1 = parent.shapeList[i].w ;
int h1 = parent.shapeList[i].h ;

Polygon p = new Polygon() ;
p.addPoint(x1, y1 + h1) ;
p.addPoint(x1 + w1, y1 + h1) ;
p.addPoint(x1 + (w1 / 2), y1) ;
p.addPoint(x1, y1 + h1) ;

if (parent.shapeList[i].shape == BasicGraphics.TRIANGLE)
g.drawPolygon(p) ;
else
g.fillPolygon(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() ;
p.setLayout(new FlowLayout()) ;
quit = new Button("Quit") ;
p.add(quit) ;
quit.addActionListener(this) ;
add("South", p) ;
} // end of constructor method

/**
* handles button depression events, etc.
*/
public void actionPerformed(ActionEvent event)
{
dispose() ;
System.exit(0) ;
} // end of method actionPerformed

/**
* set the type of shape that you want to draw
* @param shape e.g. BasicGraphics.RECTANGLE
*/
public void setShape(int shape)
{
if ((shape != RECTANGLE) && (shape != FILLED_RECTANGLE) &&
(shape != OVAL) && (shape != FILLED_OVAL) &&
(shape != TRIANGLE) && (shape != FILLED_TRIANGLE))
{
System.err.println("This is not a valid shape");
System.exit(1);
}
newShape.shape = 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 (newShape.shape == -1)
{
System.err.println("You need to set the shape first");
System.exit(1);
}
if ((x < 5) || (y < 5) || (w < 5) || (h < 5) ||
(x + w > MAX_WIDTH - 5) || (y + h > MAX_HEIGHT - 5))
{
System.err.println("Invalid dimensions supplied") ;
System.exit(1);
}
newShape.x = x ;
newShape.y = y ;
newShape.w = w ;
newShape.h = h ;
} // end of method setDimensions

/**
* set the colour of the shape that you want to draw
* @param colour the Color type (Color.red, Color.blue, etc.)
*/
public void setColour(Color colour)
{
if (newShape.x == -1)
{
System.err.println("You need to set the dimensions first");
System.exit(1);
}
newShape.colour = colour ;
shapeList[noOfShapes] = new BasicShape(newShape.shape,
newShape.x, newShape.y,
newShape.w, newShape.h,
newShape.colour) ;
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 类定义如下

选择C
原因是s是在方法中定义的变量,没有初始值,即使用之前必须先给值,要不就报错。
如果变量s在类中方法外定义有默认值null,就不会报错。

Ⅲ java中什么是类

类就是具备某些共同特征的实体的集合,它是一种抽象的数据类型,它是对所具有相同特征实体的抽象。在面向对象的程序设计语言中,类是对一类“事物”的属性与行为的抽象。举一个例子说明下类,比如Person(人)就是一个类,那么具体的某个人“张三”就是“人类”这个类的对象,而“姓名、身高、体重”等信息就是对象的属性,人的动作比如“吃饭、穿衣”等就是对象的方法。总之类就是有相同特征的事物的集合,而对象就是类的一个具体实例。同时类有多态和继承,例如“人类”可以分为“男人、女人”,“老人、小孩”那么“男人、女人”就是“人类”的子类等等。

Java语言中对类Person的定义往往如下:

public class Person {

private String name; //属性:姓名

private int height; //属性:身高

private int weight; //属性:体重

public Person() {}

public Person(String name, int height, int weight) {

this.name = name;

this.height = height;

this.weight = weight;

}

//... some methods...

public void doSth() { //行为:

//... do something

}}

Java中的类

类可以看成是创建Java对象的模板。

通过下面一个简单的类来理解下Java中类的定义:

public class Dog{
String breed; int age; String color; void barking(){
}

void hungry(){
}

void sleeping(){
}}

一个类可以包含以下类型变量:

  • 局部变量:在方法、构造方法或者语句块中定义的变量被称为局部变量。变量声明和初始化都是在方法中,方法结束后,变量就会自动销毁。

  • 成员变量:成员变量是定义在类中,方法体之外的变量。这种变量在创建对象的时候实例化。成员变量可以被类中方法、构造方法和特定类的语句块访问

  • 类变量:类变量也声明在类中,方法体之外,但必须声明为static类型。

  • 一个类可以拥有多个方法,在上面的例子中:barking()、hungry()和sleeping()都是Dog类的方法。

Ⅳ java中如何定义一个类,定义一个类需要注意那些地方

类名首字母习惯要大写,例如:HelloWorld 类名和变量名每个单词用大写字母格开, 并且变量名以小写字母开头, 如 userData;一个JAVA类文件最好有一个public类,而且只能有一个.访问权限看情况, 原则是尽可能的私有(private),不得就保护(protected),最后是公有(public). 如果是自己要封装jar,不给别人用,也可以默认,也就是包之间可以访问数据类型没什么说的吧,看基础了.变量名已经说了,变量名还有一个就是final类型的静态变量,相当于c/c++的全局变量,一般都是全部大写方法的话,除了构造函数,其它都必须要有返回值,访问权限一样,看需要其它得自己去看书了,有本叫 JAVA开发大全吧好像 里面有说到JAVA的命名规则

Ⅳ java类的里面可以再定义一个类吗

java类里面还可以定义一个类,即内部类。

  1. java内部类分为: 成员内部类、静态嵌套类、方法内部类、匿名内部类 。

  2. 内部类的共性

(1)、内部类仍然是一个独立的类,在编译之后内部类会被编译成独立的.class文件,但是前面冠以外部类的类名和$符号 。

(2)、内部类不能用普通的方式访问。内部类是外部类的一个成员,因此内部类可以自由地访问外部类的成员变量,无论是否是private的 。

(3)、内部类声明成静态的,就不能随便的访问外部类的成员变量了,此时内部类只能访问外部类的静态成员变量。

Ⅵ JAVA中类是什么意思如何定义一个类

其实,类对电脑来说,是一个数据与方法集合的模板,通过new关键字,电脑会构造一份类似的具体集合,调用对应的构造方法实现特殊性。

Ⅶ java中如何定义一个类

class {
//类体
}
用class关键字这样就可以定义一个类了

Ⅷ Java类和方法的定义

Java方法是语句的集合,它们在一起执行一个功能。

  • 方法是解决一类问题的步骤的有序组合

  • 方法包含于类或对象中

  • 方法在程序中被创建,在其他地方被引用

Java中的类

类可以看成是创建Java对象的模板

public class Dog{

String breed;

int age;

String color;

void barking(){
}

void hungry(){
}

void sleeping(){
}

}

Dog是类

barking(),hungry(),sleeping() 叫方法

Ⅸ java定义一个类

publicclassDemo{
publicstaticvoiddemo(){
Studentstudent=newStudent();
student.speak("小明",18);
}

publicstaticvoidmain(String[]args){
Demo.demo();
}
}


publicclassStudent{
Stringname=null;
intage=0;
publicvoidspeak(Stringstr,inti){
name=str;
age=i;
System.out.println("我的名字是"+name+",今年"+age+"岁");
}
}

差不多就是这个意思!!!

Ⅹ JAVA 中定义一个人的类

import java.util.Date;/**
* @author dy 定义一个“人”的类 2010-4-6下午03:56:35
*/
class Person {
private String sex;
private Date date; // 无参构造函数
public Person() { } // 有参构造函数
public Person(String sex, Date date) {
this.sex = sex;
this.date = date;
} // get()、set()方法
public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public Date getDate() {
return date;
} public void setDate(Date date) {
this.date = date;
}
}/**
* @author
* 定义“学生”类,继承“人”类
*2010-4-6下午04:01:42
*/
class Student extends Person {

//属性
private String name;
private String stuno;
private String grade;
private String stu_native;

public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getStuno() {
return stuno;
} public void setStuno(String stuno) {
this.stuno = stuno;
} public String getGrade() {
return grade;
} public void setGrade(String grade) {
this.grade = grade;
} public String getStu_native() {
return stu_native;
} public void setStu_native(String stu_native) {
this.stu_native = stu_native;
}}

热点内容
查看服务器ip限制 发布:2024-09-20 16:56:27 浏览:388
p搜系统只缓存1页为什么 发布:2024-09-20 16:48:51 浏览:838
上网的账号和密码是什么东西 发布:2024-09-20 16:31:31 浏览:612
安卓手机王者荣耀如何调超高视距 发布:2024-09-20 16:31:30 浏览:428
安卓G是什么app 发布:2024-09-20 16:23:09 浏览:81
iphone怎么压缩文件 发布:2024-09-20 16:08:18 浏览:356
linux查看用户名密码是什么 发布:2024-09-20 16:03:20 浏览:744
mac执行python脚本 发布:2024-09-20 15:58:52 浏览:779
单片机android 发布:2024-09-20 09:07:24 浏览:765
如何提高三星a7安卓版本 发布:2024-09-20 08:42:35 浏览:664