首页编程java编程java类point是什么意思(Java构造点……)

java类point是什么意思(Java构造点……)

编程之家2023-10-1192次浏览

各位老铁们,大家好,今天由我来为大家分享java类point是什么意思,以及Java构造点……的相关问题知识,希望对大家有所帮助。如果可以帮助到大家,还望关注收藏下本站,您的支持是我们最大的动力,谢谢大家了哈,下面我们开始吧!

java类point是什么意思(Java构造点……)

JAVA中构造函数是什么意思

java构造函数,也叫构造方法,java类中的一种特殊方法,方法名与类名相同,一般用来初始化一些成员变量

当要生成一个类的对象(实例)的时候就会调用类的构造函数

如果不显示声明类的构造方法,java会自动生成一个默认的不带参数的空的构造函数

java类point是什么意思(Java构造点……)

public class Test

{

public Test(){}//这里可以省略

java类point是什么意思(Java构造点……)

public static void main(String[] args)

{

Test test= new Test();

}

}

如果显示声明了类的构造函数,java就不会再生成默认的构造函数

public class Test

{

String name;

public Test(String name)//用户自定义构造函数

{

this.name= name;

}

public static void main(String[] args)

{

Test test= new Test("zhangsan");

}

}

Java构造点……

package com.point;

public class Point{

public final static int EAST= 1;//类的有名常量,用以表示的点移动方向

public final static int SOUTH=2;

public final static int WEST= 3;

public final static int NORTH=4;

private int x;

private int y;

public void set(int x, int y){

//设置点的坐标值

this.x= x;

this.y= y;

}

public void move(int orientation){

//按指定的方向将当前点移动1个像素

if(orientation==EAST){

this.x+=1;

}else if(orientation==WEST){

this.x-=1;

}else if(orientation==SOUTH){

this.y-=1;

}else{

this.y+=1;

}

}

public double distance(int x, int y){

//计算当前点与指定点之间的距离

return Math.sqrt((this.x-x)*(this.x-x)+(this.y-y)*(this.y-y));

}

public boolean equals(Point p){

//测试当前点与参数点p的坐标值是否相等

return this.x==p.getX()&&this.y==p.getY();

}

public int getX(){

//返回当前点的X坐标

return this.x;

}

public int getY(){

//返回当前点的Y坐标

return this.y;

}

public String toString(){

//以(x,y)格式返回当前点对象的字符串表示

return"("+this.x+","+this.y+")";

}

}

//main测试

package com.point;

public class Test{

public static void main(String[] args){

Point point= new Point();

point.set(5, 5);

System.out.println("当前位置:"+point);

//向东移动

point.move(Point.EAST);

System.out.println("向东移动之后的位置:"+point);

//向西移动

point.move(Point.WEST);

System.out.println("向西移动之后的位置:"+point);

//向南移动

point.move(Point.SOUTH);

System.out.println("向南移动之后的位置:"+point);

//向北移动

point.move(Point.NORTH);

System.out.println("向北移动之后的位置:"+point);

//两点距离

System.out.println(point+"和(8,9)亮点距离:"+point.distance(8, 9));

Point comPoint= new Point();

comPoint.set(9, 9);

//对比两点

System.out.println("对比"+point+"和"+comPoint+"是否为同一点:"+point.equals(comPoint));

}

}

//测试结果

当前位置:(5,5)

向东移动之后的位置:(6,5)

向西移动之后的位置:(5,5)

向南移动之后的位置:(5,4)

向北移动之后的位置:(5,5)

(5,5)和(8,9)亮点距离:5.0

对比(5,5)和(9,9)是否为同一点:false

java内省和反射的区别

经过多方面的资料搜集整理,写下了这篇文章,本文主要讲解java的反射和内省机制,希望对大家有点帮助,也希望大家提出不同的看法!

1).内省(Introspector)是 Java语言对 Bean类属性、事件的一种缺省处理方法。例如类 A中有属性 name,那我们可以通过 getName,setName来得到其值或者设置新的值。通过 getName/setName来访问 name属性,这就是默认的规则。 Java中提供了一套 API用来访问某个属性的 getter/setter方法,通过这些 API可以使你不需要了解这个规则(但你最好还是要搞清楚),这些 API存放于包 java.beans中。

2).直接通过属性的描述器java.beans.PropertyDescriptor类,来访问属性的getter/setter方法;

相关代码:

public class Point{

private Integer x;

private Integer y;

public Point(Integer x, Integer y){

super();

this.x= x;

this.y= y;

}

public Integer getX(){

return x;

}

public void setX(Integer x){

this.x= x;

}

public Integer getY(){

return y;

}

public void setY(Integer y){

this.y= y;

}

}

import java.beans.PropertyDescriptor;

import java.lang.reflect.Method;

public class Reflect{

public static void main(String[] args) throws Exception{

Point point= new Point(2, 5);

String proName="x";

getProperty(point, proName);

setProperty(point, proName);

}

private static void setProperty(Point point, String proName) throws Exception{

PropertyDescriptor proDescriptor= new PropertyDescriptor(proName, Point.class);

Method methodSetX= proDescriptor.getWriteMethod();

methodSetX.invoke(point, 8);

System.out.println(point.getX());// 8

}

private static void getProperty(Point point, String proName) throws Exception{

PropertyDescriptor proDescriptor= new PropertyDescriptor(proName, Point.class);

Method methodGetX= proDescriptor.getReadMethod();

Object objx= methodGetX.invoke(point);

System.out.println(objx);// 2

}

}

3).通过类 Introspector来获取某个对象的 BeanInfo信息,然后通过 BeanInfo来获取属性的描述器( PropertyDescriptor),通过这个属性描述器就可以获取某个属性对应的 getter/setter方法,然后我们就可以通过反射机制来调用这些方法。

相关代码:

把2中的getProperty()修改成如下形式:

private static void getProperty(Point point, String proName) throws Exception{

BeanInfo beanInfo= Introspector.getBeanInfo(point.getClass());

PropertyDescriptor[] proDescriptors= beanInfo.getPropertyDescriptors();

for(PropertyDescriptor prop: proDescriptors){

if(prop.getName().equals(proName)){

Method methodGetx= prop.getReadMethod();

System.out.println(methodGetx.invoke(point));//8

break;

}

}

}

4).我们又通常把javabean的实例对象称之为值对象(Value Object),因为这些bean中通常只有一些信息字段和存储方法,没有功能性方法。一个JavaBean类可以不当JavaBean用,而当成普通类用。JavaBean实际就是一种规范,当一个类满足这个规范,这个类就能被其它特定的类调用。一个类被当作javaBean使用时,JavaBean的属性是根据方法名推断出来的,它根本看不到java类内部的成员变量(javabean的成员变量通常都是私有private的)。

5).除了反射用到的类需要引入外,内省需要引入的类如下所示,它们都属于java.beans包中的类,自己写程序的时候也不能忘了引入相应的包或者类。

import java.beans.BeanInfo;

import java.beans.IntrospectionException;

import java.beans.Introspector;

import java.beans.PropertyDescriptor;

6).下面讲解一些开源的工具类Beanutils,需要额外下载的,commons-beanutils.jar,要使用它还必须导入commons-logging.jar包,不然会出异常;

相关代码一:

public static void main(String[] args) throws Exception{

Point point= new Point(2, 5);

String proName="x";

BeanUtils.setProperty(point, proName,"8");

System.out.println(point.getX());// 8

System.out.println(BeanUtils.getProperty(point, proName));// 8

System.out.println(BeanUtils.getProperty(point, proName).getClass().getName());// java.lang.String

BeanUtils.setProperty(point, proName, 8);

System.out.println(BeanUtils.getProperty(point, proName).getClass().getName());// java.lang.String

}

//我们看到虽然属性x的类型是Integer,但是我们设置的时候无论是Integer还是String,BeanUtils的内部都是当成String来处理的。

相关代码二:

BeanUtils支持javabean属性的级联操作;

public static void main(String[] args) throws Exception{

Point point= new Point(2, 5);//在point中加一个属性 private Date birth= new Date();并产生setter/getter方法

String proName="birth";

Date date= new Date();

date.setTime(10000);

BeanUtils.setProperty(point, proName, date);

System.out.println(BeanUtils.getProperty(point, proName));

BeanUtils.setProperty(point,"birth.time", 10000);

System.out.println(BeanUtils.getProperty(point,"birth.time"));//10000

}

//之所以可以 BeanUtils.setProperty(point,"birth.time", 10000);这样写,那是因为Date类中有getTime()和setTime()方法,即Date类中相当于有time这个属性。

相关代码三:

BeanUtils和PropertyUtils对比:

public static void main(String[] args) throws Exception{

Point point= new Point(2, 5);

String proName="x";

BeanUtils.setProperty(point, proName,"8");

System.out.println(BeanUtils.getProperty(point, proName));//8

System.out.println(BeanUtils.getProperty(point, proName).getClass().getName());//java.lang.String

// PropertyUtils.setProperty(point, proName,"8");//exception:argument type mismatch

PropertyUtils.setProperty(point, proName, 8);

System.out.println(PropertyUtils.getProperty(point, proName));//8

System.out.println(PropertyUtils.getProperty(point, proName).getClass().getName());//java.lang.Integer

}

//BeanUtils它以字符串的形式对javabean进行转换,而PropertyUtils是以原本的类型对javabean进行操作。如果类型不对,就会有argument type mismatch异常。

6).理解了相应的原理,那些现成的工具用起来就会更舒服,如Beanutils与PropertyUtils工具。这两个工具设置属性的时候一个主要区别是PropertyUtils.getPropety方法获得的属性值的类型为该属性本来的类型,而BeanUtils.getProperty则是将该属性的值转换成字符串后才返回。

Web开发框架 Struts中的 FormBean就是通过内省机制来将表单中的数据映射到类的属性上,因此要求 FormBean的每个属性要有 getter/setter方法。但也并不总是这样,什么意思呢?就是说对一个 Bean类来讲,我可以没有属性,但是只要有 getter/setter方法中的其中一个,那么 Java的内省机制就会认为存在一个属性,比如类中有方法 setMobile,那么就认为存在一个 mobile的属性。

将 Java的反射以及内省应用到程序设计中去可以大大的提供程序的智能化和可扩展性。有很多项目都是采取这两种技术来实现其核心功能,例如我们前面提到的 Struts,还有用于处理 XML文件的 Digester项目,其实应该说几乎所有的项目都或多或少的采用这两种技术。在实际应用过程中二者要相互结合方能发挥真正的智能化以及高度可扩展性。

java中float和double的取值范围是什么

float:4字节(32bit),IEEE 754.取值范围:

[-3.40282346638528860e+38,-1.40129846432481707e-45]∪ [1.40129846432481707e-45~ 3.40282346638528860e+38]。

double: 8字节(64bit),IEEE 754.取值范围:

[-1.79769313486231570e+308,-4.94065645841246544e-324]∪ [4.94065645841246544e-324,1.79769313486231570e+308]。

扩展资料

基本数据类型的特点,位数,最大值和最小值。

1、

基本类型:short二进制位数:16

包装类:java.lang.Short

最小值:Short.MIN_VALUE=-32768(-2的15此方)

最大值:Short.MAX_VALUE=32767(2的15次方-1)

2、

基本类型:int二进制位数:32

包装类:java.lang.Integer

最小值:Integer.MIN_VALUE=-2147483648(-2的31次方)

最大值:Integer.MAX_VALUE= 2147483647(2的31次方-1)

3、

基本类型:long二进制位数:64

包装类:java.lang.Long

最小值:Long.MIN_VALUE=-9223372036854775808(-2的63次方)

最大值:Long.MAX_VALUE=9223372036854775807(2的63次方-1)

4、

基本类型:float二进制位数:32

包装类:java.lang.Float

最小值:Float.MIN_VALUE=1.4E-45(2的-149次方)

最大值:Float.MAX_VALUE=3.4028235E38(2的128次方-1)

5、

基本类型:double二进制位数:64

包装类:java.lang.Double

最小值:Double.MIN_VALUE=4.9E-324(2的-1074次方)

最大值:Double.MAX_VALUE=1.7976931348623157E308(2的1024次方-1)

参考资料:Java官网-Java教程

OK,关于java类point是什么意思和Java构造点……的内容到此结束了,希望对大家有所帮助。

java游戏有什么不一样吗?好玩的JAVA游戏有哪些java中类的属性是什么意思 java中类定义的属性是什么意思