int i=10;
System.out.println((float)i);这里的(float)i是永远把i转换成float类型还是只是这个时候转换? 我自己测试了一下,应该是暂时转换 不过 吃不准,就上来问问。另一个问题书上写到静态方法里的方法也一定要是静态的才可以
可是为什么main方法里可以调用对象的非静态方法?最后一个。关于equals的问题
class Employee { int age;
String name;
Employee(int age,String name)
{
this.age=age;
this.name=name;
}
public static void main(String args[])
{
Employee a=new Employee(1,"aaaa");
a b=new a(2,"bbbb");
System.out.println(a.equals(b)); //这里 ,我想知道这里到底比的是什么?难道是这两个对象里的两个成员分别相比么?
}
}class a
{
int nianling;
String xingming;
a(int nianling,String xingming)
{
this.nianling=nianling;
this.xingming=xingming;
}
}
System.out.println((float)i);这里的(float)i是永远把i转换成float类型还是只是这个时候转换? 我自己测试了一下,应该是暂时转换 不过 吃不准,就上来问问。另一个问题书上写到静态方法里的方法也一定要是静态的才可以
可是为什么main方法里可以调用对象的非静态方法?最后一个。关于equals的问题
class Employee { int age;
String name;
Employee(int age,String name)
{
this.age=age;
this.name=name;
}
public static void main(String args[])
{
Employee a=new Employee(1,"aaaa");
a b=new a(2,"bbbb");
System.out.println(a.equals(b)); //这里 ,我想知道这里到底比的是什么?难道是这两个对象里的两个成员分别相比么?
}
}class a
{
int nianling;
String xingming;
a(int nianling,String xingming)
{
this.nianling=nianling;
this.xingming=xingming;
}
}
解决方案 »
- 关于《think in java》中的泛型数组(关于擦除的问题) 求大牛指教!!!
- 试用Socket写QQ程序,但出现阻塞问题,希望高手指教。
- 子类对象引用问题
- 接口方法的abstract关键字是什么意思
- 用SimpleDateFormat将1900-01-01 8:00:00转化后相差5:52
- 我用 DefaultTableModel 给jtable添加了数据,如何禁止在表格中编辑的功能呢
- 郁闷,用Jtable显示数据库中的记录,在applet中运行良好,到IE中就不能使了
- JBuilder编辑快捷键技巧,大家来参考
- 郁闷,告诉自己要有耐心,诸位仁兄、仁姐帮个忙(在线)。
- 哪里有java的编程例子的书籍
- 字符串池问题
- 请问怎么读取jar内部的resource文件?
是暂时转换
第二个问题
静态方法里一定要调用静态方法
main()方法中生成其他类的对象后,就可以调用对象的非静态方法
第三个不太明白,应该是吧
float temp = (float)i;
System.out.println(temp);2, If a static method invoke another method directly, without using any reference variable, then the invoked method must be static too.3, This cannot be explained in one sentence. Be default, if you compare obj1 with obj2 using obj1.equals(obj2), the result is same as using obj1 == obj2. The later is checking whether obj1 and obj2 are referring to the same object instance.If you need a customized way to compare objects, you have to override the equals(Object o) method using customized logic.For example, let's compare whether two Circle are the same.class Circle {
private double radius;
public Circle();
public Circle(double r) {
this.radius = r >= 0 ? r : 0;
} // If they have the same radius, we say they are equal.
public boolean equals(Object o) {
return (o instanceof Circle) && this.radius == ((Circle)o).radius;
}
}
System.out.println((float)i);=====>这只是一个暂时的转换,int i =10;
float a = (float)i;=====>这里只是把i转换成float,但是i本身的值并没变.如果在你自己写类里面有一个静态方法,那么这个静态方法不可以返回实例域的成员,
也就是:private int a ;
public static final int a = 10;
public static int getA(){ return a;========>着是错误的,如果把static去掉,就可以返回了.如果在实例域里面有一个静态成员,如上的静态方法.那么这样写,就成立.否则就错误.
}
至于main方法,它本身就是一个静态方法,所以他可以通过类的名称来直接调用这个方法,如:public static void main(String[] args){ 自己定义的一个类<===Test.getA();======>就当上面的getA()方法是静态的,那么这里就可以这样调用了,如果他没有写static,那么必须这样调用.如下: Test a = new Test();
a.getA();
}equals方法,平时你写equals的时候,那是基础类库里面的,他们所对应的都是那些类,比如String类里面有equals方法,他只能比较两个字符是否相等.然后,你的equals方法是继承与Object类的equals方法,Object是所有类的超级类,也就是说,你自定义的类,都是继承了这个类而来的,他里面的equals是这样写的,
public boolean equals(Object other){
this == other;}
在==号.在JAVA对象里面,所表示的对象是否指向同一个地方.所以不能用Object类的equals方法来判断你自己定义的类,这样就必须要重写equals方法了,假如你写个类.public class Test{ private String name;
private int salary; public Test(String name, int salary){ this.name = name;
this.salary = salary;
} public boolean equals(Test other){ if(this == other) return true;
if(other == null) return false;====>这就是自己重写equals方法了,这些方法要你看你需要怎么写的,就可以怎么写. return name.equals(other.name) && salary == other.salary; }}
第二个问题:必须静态
第三个问题:如果不重写equal()方法,比较的是两个对象的引用是否相等,跟“==”一样!
if(other == null) return false;====>这就是自己重写equals方法了,这些方法要你看你需要怎么写的,就可以怎么写. return name.equals(other.name) && salary == other.salary; }这个没看明白。 。。