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() 叫方法