「java中类定义」Java中类的定义
本篇文章给大家谈谈java中类定义,以及Java中类的定义对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
Java中的类指的是什么?
1.JAVA中的类是具备某些共同特征的实体的集合,它是一种抽象的概念;
2.用程序设计的语言来说,类是一种抽象的数据类型,它是对所具有相同特征实体的抽象;
3.所谓对象就是真实世界中的实体,对象与实体是一一对应的,也就是说现实世界中每一个实体都是一个对象,对象是一种具体的概念。
4.类是对象的集合,对象是类的实例;对象是通过new className产生的,用来调用类的方法;类的构造方法 。
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中的类是什么意思?
Java创建一个类的关键字为class,基本语法格式为public class ClassName{};在开发中,通常类的名字首字母大写。类中包含了类的特定属性,比如我们创建一个动物类,动物有自己的属性名字,年龄等属性特点,我们创建好动物这个类之后,当需要一个动物的时候,就去创建一个动物的对象,之后使用这个具体的对象进行操作就可以。创建对象的关键字是new,基本语法为 ClassName objectName = new ClassName();
在Java中类的构造函数包含有参构造函数和无参构造函数,默认如果不在类中写构造函数,默认有一个无参的构造函数,当创建一个对象的时候,默认使用的就是这个构造函数。
如果需要在创建对象的时候就将对象的属性值设置好,就需要一个有参构造函数,在创建对象的时候,将参数传入即可。如果创建一个有参的构造函数,那么默认的无参构造函数将会被覆盖,如果还需要一个无参构造函数,则需要手动重写一个无参构造函数。
Java类可以被继承,子类会继承父类的一些属性值,但是子类也可以有自己的一些特定属性,小狗(子类)属于动物(父类),有动物这个种类的所有属性,但是小狗也有属于自己的特性。在开发中我们会将具有某些基本属性的归为一类,如果另一个类也有这些属性,而且它还有属于自己的特性,可以将子类继承于父类,这样子类就包含了父类所有的属性。
Java类的继承关键字是extends,基本语法为: public class ChiledClass extends ParentClass{};
在子类中,有时候虽然继承了父类的属性值,但是有时候,我们需要子类中的该属性值有自己的特点,这时候,我们可以重写父类的属性方法,将该属性设置为我们需要的值,这样虽然子类继承于父类,但是也有自己的独特性。
在开发中,我们通常会将类的属性设置为私有的,这样外部就不会随意访问到这个属性。但是为了让外部能够访问该属性值,我们通常使用 set和get方法去设置和获取该属性值,这样如果开发中不想让别人随意修改该属性,可以将set方法去掉,只留下get方法,这样这个属性就只可以访问而不可以修改,很好的保护的这个属性值,不让别人去随意修改。
java中对于类的定义有哪些?
类的设计思想:
构造函数,可以给点赋初值的构造函数。
点的相关参数作为成员变量,如横坐标,纵坐标。
设计成员函数,如取得坐标值的函数,此点和原点之间的距离。
in c language
123456789101112131415161718192021222324
class Point{ protected: double x; double y; Point(inx = 0 , iny = 0){//C的缺省构造函数,构造不传入参数时默认值是0 x = inx; y = iny; } ~Point(){ } public: double getX(){ return x; } double getY(){ return y; } void setX(double inx){ x = inx; } void setY(double iny){ y = iny; }};//写C一定不要忘记,类定义结束也有个分号。
in java language
1234567891011121314151617181920
public class Point(){//JAVA对于类的属性要求很严苛,每个元素必须显式注明属性 protected double x; protected double y; public Point(double inx,double iny){ x = inx; y = iny; } public void setX(double inx){ x = inx; } public void setY(double iny){ y = iny; } public double getX(){ return x; } public double getY(){ return y; }}
in JS
1234567891011121314151617
\*JS中万物皆为obj,而没有CLASS, 你可以认为FUNCTION就是一个OBJ *\function Point(inx,iny){//js就比较宽松了,可以随意申请变量,但是JS没有保护这个概念 var x = inx; \\这就是一个私有变量。只能在类内部访问 var y = iny; this.outx = x; \\共有变量,在外部访问时 obj.name 内部访问this.name this.outy = y; function setX(inx){ \\JS有setget设置器,但是如果用那个,看的不是很清楚 this.outx = inx; x = inx; } function setY(iny){ this.outy = iny; y = iny; }}\\对于JS,类的基础源于其原型关键字,这一块可以参考W3CSCHOOL\\
in PHP
123456789101112131415161718192021222324
?phpclass Point{ protected $x; protected $y; function __construct($inx,$iny){ $this-x = $inx; $this-y = $iny; } function __destruct(){} public function getX(){ return $this-x; } public function getY(){ return $this-y; } public function setX($inx){ $this-x = $inx; } public function setY(){ $this-y = $iny; }}?
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中类的定义的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-12-11,除非注明,否则均为
原创文章,转载请注明出处。