java 中&的含义
答案:3 悬赏:60
解决时间 2021-04-28 05:00
- 提问者网友:痞子房西
- 2021-04-27 07:11
int a = 3;int b = 2;a&b=?什么时候为0?true&true= true对于int呢?什么规则
最佳答案
- 二级知识专家网友:初心未变
- 2021-04-27 08:32
&的作用是把两个操作数按位做与运算,就上面的例子来说,a是3,它的二进制数为00000011,b为2,它的二进制数为00000010,两个数按位做与运算后,得到的结果为00000010
即2。做与运算时,只有当两个数都为1时结果才是1,其他情况都是0。
注意:在JAVA中,int实际上是32位的,这里是举例,故只取其低8位。
全部回答
- 1楼网友:陪衬角色
- 2021-04-27 10:30
A&B=1
- 2楼网友:一池湖水
- 2021-04-27 09:52
1、在一个类中,this可以表示该类的当前实例;例如:
public class leaf {
private int i = 0;
leaf increment() {
i++;
return this;
}
void print() {
system.out.println("i = " + i);
}
public static void main(string[] args) {
leaf x = new leaf();
x.increment().increment().increment().print();
}
} ///:~ ...展开1、在一个类中,this可以表示该类的当前实例;例如:
public class leaf {
private int i = 0;
leaf increment() {
i++;
return this;
}
void print() {
system.out.println("i = " + i);
}
public static void main(string[] args) {
leaf x = new leaf();
x.increment().increment().increment().print();
}
} ///:~
2、若为一个类写了多个构造器,那么经常都需要在一个构造器里调用另一个构造器,以避免写重复的代码。这时可以使用this,例如:
//: flower.java
// calling constructors with "this"
public class flower {
private int petalcount = 0;
private string s = new string("null");
flower(int petals) {
petalcount = petals;
system.out.println(
"constructor w/ int arg only, petalcount= "
+ petalcount);
}
flower(string ss) {
system.out.println(
"constructor w/ string arg only, s=" + ss);
s = ss;
}
flower(string s, int petals) {
this(petals);
//! this(s); // can''t call two!
this.s = s; // another use of "this"
system.out.println("string & int args");
}
flower() {
this("hi", 47);
system.out.println(
"default constructor (no args)");
}
void print() {
//! this(11); // not inside non-constructor!
system.out.println(
"petalcount = " + petalcount + " s = "+ s);
}
public static void main(string[] args) {
flower x = new flower();
x.print();
}
} ///:~
而需要注意的是:在一个类a的内部类b中使用this它表示的并非是a.b的当前实例,而是a的当前实例;收起
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息