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

类是一种引用数据类型。类为对象的模板,简单的说就是分类。
类的定义包括“回成员变量”答的定义和“方法”的定义,其中“成员变量”用于描述一类对象共同的数据结构。在Java语言中,类的成员变量的定义可以使用如下语法:
class 类名 {
成员变量类型变量名称;
………
}

类是用class关键字来定义的一种抽象数据类型,类不但定义了抽象数据类型的组成(成员变量),同时还定义了对该类型可以实施的操作(方法),类名的首字母必须大写。看如下代码定义了雇员类:
/** 定义雇员类 */
public class Emp{
String name;
int age;
char gender;
double salary;
}

在如上的实例代码中,仅仅定义了Emp类型的组成,即成员变量。该类定义了4个成员变量:String类型的name用于存放名字;int类型的age用于存放年龄;char类型的gender用于存放性别;double类型的salary用于存放工资。

❷ 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的类名

你这不是类名,是源文件名(不过源文件的名字必须是主类的名字,所以,也算是类名吧)。
命名规则我们要保持如下习惯:
第一,类名是由字母、数字、下划线或美元符组成,且第一个字符不能是数字。
第二,如果用的是拉丁字母,那么名字的首字母使用大写字母。
第三,如果多个单词复合而成,我们习惯上,每个单词的第一个字母大写。

所以说,你定义的JAVA名可以。

❹ 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类定义

Queue是LinkedList的父接口,也可以是父类。

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

这个真的不太好解释,类这个概念几乎贯穿了整个java,其实这个概念贯穿了所有的面向对象的程序语言。

你可以把类看成是蓝图,设计图,对一类事物或者意图的某些方面的描述。比如你要描述一种汽车,当然汽车有太多的属性了,但你在意的恐怕只有几种而已。

怎么定义一个类,就是使用class关键字来定义,给你看几个类,如下:

publicclassSchoolGirl{
privateStringname;
publicSchoolGirl(Stringname){
this.name=name;
}
publicStringgetName(){
returnthis.name;
}

}

上面这个类是很基本的类的定义,有一个私有成员变量name, 有一个带参数的构造方法,有一个get方法,当然,你还可以设置其他的成员变量和成员方法。我们再看一个:

publicclassAudioPlayer{
publicvoidplay(){
System.out.println("play");
}
publicvoidrewind(){
System.out.println("rewind");
}
publicvoidstop(){
System.out.println("stop");
}

}

以上这个呢,也是一个类,可是里面没什么东西,只有三个成员方法,其实这也是可以的。

具体的类的作用,定义,使用方法,并不是一两句能说明的。

❼ 关于JAVA的问题:什么是类写出java中类定义的格式

类(抄Class)是面向对象程袭序设计(OOP,Object-Oriented Programming)实现信息封装的基础。类是一种用户定义的引用数据类型,也称类类型。每个类包含数据说明和一组操作数据或传递消息的函数。类的实例称为对象。

定义一个类的格式如下图:

(7)类的定义java扩展阅读:

对象可以访问类的成员,但并不是所有成员都可以被访问,能否访问取决于声明该成员时所用的关键字(public/protected/private)。具体规则如下:

1、类的公有成员可以被该类,其派生类和类实例化的对象访问。

2、类的保护成员可以被该类及其派生类访问,不可以被该类的对象访问。

3、类的私有成员可以被该类访问,不可以被派生类及其该类的对象访问。

❽ java 定义类 如何写

类是一种引用数据类型。类为对象的模板,简单的说就是分类。

类的定义包回括“成员变答量”的定义和“方法”的定义,其中“成员变量”用于描述一类对象共同的数据结构。在Java语言中,类的成员变量的定义可以使用如下语法:

class 类名 {

成员变量类型变量名称;

………

}

类是用class关键字来定义的一种抽象数据类型,类不但定义了抽象数据类型的组成(成员变量),同时还定义了对该类型可以实施的操作(方法),类名的首字母必须大写。看如下代码定义了雇员类:

/** 定义雇员类 */

public class Emp{

String name;

int age;

char gender;

double salary;

}

在如上的实例代码中,仅仅定义了Emp类型的组成,即成员变量。该类定义了4个成员变量:String类型的name用于存放名字;int类型的age用于存放年龄;char类型的gender用于存放性别;double类型的salary用于存放工资。

❾ Java的定义类

呐呐虽然不知道你要干嘛,我就手打了一段代码给你,应该是没有版错误的,没调试过,毕竟很简权单。
class LastCall{
public String LastCalled = "";

public static void yoo(){
this.LastCalled = "yoo";

}
public static void hoo(){
this.LastCalled = "hoo";

}
public static void foo(){
this.LastCalled = "foo";

}
public static String getlast(){
return this.LastCalled;
}

}

❿ Java类和方法的定义

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

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

  • 方法包含于类或对象中

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

Java中的类

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

public class Dog{

String breed;

int age;

String color;

void barking(){
}

void hungry(){
}

void sleeping(){
}

}

Dog是类

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