java類的定義,java類的定義和使用示例

本文目錄一覽:

在Java中具體講解什麼叫做類

嗨 你好啊

類可解為以下:

類(Class)實際上是對某種類型的對象定義變量和方法的原型。它表示對現實生活中一類具有共同特徵的事物的抽象,是面向對象編程的基礎。

類是對某個對象的定義。它包含有關對象動作方式的信息,包括它的名稱、方法、屬性和事件。實際上它本身並不是對象,因為它不存在於內存中。當引用類的代碼運行時,類的一個新的實例,即對象,就在內存中創建了。雖然只有一個類,但能從這個類在內存中創建多個相同類型的對象。類通過接口與外界發生關係。

可以把類看作“理論上”的對象,也就是說,它為對象提供藍圖,但在內存中並不存在。從這個藍圖可以創建任何數量的對象。從類創建的所有對象都有相同的成員:屬性、方法和事件。但是,每個對象都象一個獨立的實體一樣動作。例如,一個對象的屬性可以設置成與同類型的其他對象不同的值。

希望可以給你在學習Java的路上帶來力所能及的幫助

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語言中,類的成員變量的定義可以使用如下語法:

class 類名 {

成員變量類型變量名稱;

………

}

類是用class關鍵字來定義的一種抽象數據類型,類不但定義了抽象數據類型的組成(成員變量),同時還定義了對該類型可以實施的操作(方法),類名的首字母必須大寫。看如下代碼定義了僱員類:

/** 定義僱員類 */

public class Emp{

String name;

int age;

char gender;

double salary;

}

在如上的實例代碼中,僅僅定義了Emp類型的組成,即成員變量。該類定義了4個成員變量:String類型的name用於存放名字;int類型的age用於存放年齡;char類型的gender用於存放性別;double類型的salary用於存放工資。

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/292059.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-25 14:07
下一篇 2024-12-25 14:07

相關推薦

  • java client.getacsresponse 編譯報錯解決方法

    java client.getacsresponse 編譯報錯是Java編程過程中常見的錯誤,常見的原因是代碼的語法錯誤、類庫依賴問題和編譯環境的配置問題。下面將從多個方面進行分析…

    編程 2025-04-29
  • Java JsonPath 效率優化指南

    本篇文章將深入探討Java JsonPath的效率問題,並提供一些優化方案。 一、JsonPath 簡介 JsonPath是一個可用於從JSON數據中獲取信息的庫。它提供了一種DS…

    編程 2025-04-29
  • Java Bean加載過程

    Java Bean加載過程涉及到類加載器、反射機制和Java虛擬機的執行過程。在本文中,將從這三個方面詳細闡述Java Bean加載的過程。 一、類加載器 類加載器是Java虛擬機…

    編程 2025-04-29
  • Java騰訊雲音視頻對接

    本文旨在從多個方面詳細闡述Java騰訊雲音視頻對接,提供完整的代碼示例。 一、騰訊雲音視頻介紹 騰訊雲音視頻服務(Cloud Tencent Real-Time Communica…

    編程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介紹

    本文將詳細介紹Java Milvus SearchParam withoutFields的相關知識和用法。 一、什麼是Java Milvus SearchParam without…

    編程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java語言中的一個版本,於2014年3月18日發布。本文將從多個方面對Java 8中某一周的周一進行詳細的闡述。 一、數組處理 Java 8新特性之一是Stream…

    編程 2025-04-29
  • Python3定義函數參數類型

    Python是一門動態類型語言,不需要在定義變量時顯示的指定變量類型,但是Python3中提供了函數參數類型的聲明功能,在函數定義時明確定義參數類型。在函數的形參後面加上冒號(:)…

    編程 2025-04-29
  • Java判斷字符串是否存在多個

    本文將從以下幾個方面詳細闡述如何使用Java判斷一個字符串中是否存在多個指定字符: 一、字符串遍歷 字符串是Java編程中非常重要的一種數據類型。要判斷字符串中是否存在多個指定字符…

    編程 2025-04-29
  • VSCode為什麼無法運行Java

    解答:VSCode無法運行Java是因為默認情況下,VSCode並沒有集成Java運行環境,需要手動添加Java運行環境或安裝相關插件才能實現Java代碼的編寫、調試和運行。 一、…

    編程 2025-04-29
  • Java任務下發回滾系統的設計與實現

    本文將介紹一個Java任務下發回滾系統的設計與實現。該系統可以用於執行複雜的任務,包括可回滾的任務,及時恢復任務失敗前的狀態。系統使用Java語言進行開發,可以支持多種類型的任務。…

    編程 2025-04-29

發表回復

登錄後才能評論