當前位置:首頁 » 編程語言 » 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;
}}

熱點內容
單片機android 發布:2024-09-20 09:07:24 瀏覽:765
如何提高三星a7安卓版本 發布:2024-09-20 08:42:35 瀏覽:664
如何更換伺服器網站 發布:2024-09-20 08:42:34 瀏覽:311
子彈演算法 發布:2024-09-20 08:41:55 瀏覽:289
手機版網易我的世界伺服器推薦 發布:2024-09-20 08:41:52 瀏覽:817
安卓x7怎麼邊打游戲邊看視頻 發布:2024-09-20 08:41:52 瀏覽:162
sql資料庫安全 發布:2024-09-20 08:31:32 瀏覽:94
蘋果連接id伺服器出錯是怎麼回事 發布:2024-09-20 08:01:07 瀏覽:507
編程鍵是什麼 發布:2024-09-20 07:52:47 瀏覽:658
學考密碼重置要求的證件是什麼 發布:2024-09-20 07:19:46 瀏覽:481