當前位置:首頁 » 編程語言 » java類的定義

java類的定義

發布時間: 2022-01-15 15:25:37

java中類是什麼意思如何定義一個類

其實,類對電腦來說,是一個數據與方法集合的模板,通過new關鍵字,電腦會構造一份類似的具體集合,調用對應的構造方法實現特殊性。

❷ 關於JAVA的問題:什麼是類寫出java中類定義的格式

類(Class)是面向對象程序設計(OOP,Object-Oriented Programming)實現信息封裝的基礎。類是一種用戶定義的引用數據類型,也稱類類型。每個類包含數據說明和一組操作數據或傳遞消息的函數。類的實例稱為對象。

定義一個類的格式如下圖:

(2)java類的定義擴展閱讀:

對象可以訪問類的成員,但並不是所有成員都可以被訪問,能否訪問取決於聲明該成員時所用的關鍵字(public/protected/private)。具體規則如下:

1、類的公有成員可以被該類,其派生類和類實例化的對象訪問。

2、類的保護成員可以被該類及其派生類訪問,不可以被該類的對象訪問。

3、類的私有成員可以被該類訪問,不可以被派生類及其該類的對象訪問。

❸ Java類和方法的定義

Java方法是語句的集合,它們在一起執行一個功能。

  • 方法是解決一類問題的步驟的有序組合

  • 方法包含於類或對象中

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

Java中的類

類可以看成是創建Java對象的模板

public class Dog{

String breed;

int age;

String color;

void barking(){
}

void hungry(){
}

void sleeping(){
}

}

Dog是類

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

❹ java類的裡面可以再定義一個類嗎

java類裡面還可以定義一個類,即內部類。

  1. java內部類分為: 成員內部類、靜態嵌套類、方法內部類、匿名內部類 。

  2. 內部類的共性

(1)、內部類仍然是一個獨立的類,在編譯之後內部類會被編譯成獨立的.class文件,但是前面冠以外部類的類名和$符號 。

(2)、內部類不能用普通的方式訪問。內部類是外部類的一個成員,因此內部類可以自由地訪問外部類的成員變數,無論是否是private的 。

(3)、內部類聲明成靜態的,就不能隨便的訪問外部類的成員變數了,此時內部類只能訪問外部類的靜態成員變數。

❺ Java中,類的定義中包括那些基本信息

public(許可權修飾符,不寫為friendly),static(修飾方法可以直接用類名調用,可選),返回值(數據基本類型,或者void),最後方法名

❻ 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定義一個類

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中類的概念

舉個例子說吧
有個汽車類
它有屬性和方法
它的屬性就像幾個輪子 幾個車燈 什麼顏色 車把手是方的還是園的等等
它的方法像 它能拉貨物的方法, 又有拉幾個男人的方法,拉幾個女人的方法等等
當這個人創建對象的時候 就是造出來一輛車 賓士 或者寶馬 呵呵

❾ 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類的定義及使用

publicclassCylinder{
doubleradius;
doubleheight;

publicCylinder(){
this.radius=0;
this.height=0;
}

publicCylinder(doubleradius,doubleheight){
this.radius=radius;
this.height=height;
}

publicdoublecubage(){
return3.1415926*radius*radius*height;
}
@Override
publicStringtoString(){
return"圓柱體的半徑為:"+radius+","+"高為:"+
height+","+"體積為:"+cubage();
}
}

熱點內容
無許可權訪問工作組的計算機 發布:2024-09-21 04:26:31 瀏覽:475
為什麼ipad需要密碼解鎖 發布:2024-09-21 04:06:22 瀏覽:210
mariadb存儲過程 發布:2024-09-21 03:56:05 瀏覽:514
壓縮殼脫殼機 發布:2024-09-21 03:14:33 瀏覽:93
熱血街籃為什麼是伺服器維護中 發布:2024-09-21 03:08:19 瀏覽:937
喇叭怎麼配置功放 發布:2024-09-21 03:06:50 瀏覽:751
為什麼安卓的內存沒有蘋果的內存 發布:2024-09-21 03:06:50 瀏覽:231
swift解壓 發布:2024-09-21 02:31:47 瀏覽:706
移動中心怎麼配置安卓系統 發布:2024-09-21 02:27:16 瀏覽:607
安卓手機舊版app怎麼下載 發布:2024-09-21 02:12:35 瀏覽:801