关于java路径中的空格问题 中类的一个问题

关于java,在一个类中调用另一个类中的成员变量及方法的问题?_百度知道
关于java,在一个类中调用另一个类中的成员变量及方法的问题?
t.live=和t.setlive(true);public Boolean setlive(Boolean live) { }直接赋值和调用方法方法的返回值,这两个有什么区别?
java是面向对象的,面向对象的特征之一就是封装,意思是隐藏该隐藏的,暴露该暴露的,java中有一个术语叫做JAVABEAN,java推荐一个java类中的字段都应该是private的,private修饰的无法被其他类访问,这样就隐藏了字段,而想操作该字段,则使用set方法来操作,set方法为public,对其他类来说,该方法可以访问,这样就暴露了该暴露的,这样的设计是符合java的设计理念和面向对象的设计原则的。区别就在这里
其他类似问题
为您推荐:
成员变量的相关知识
其他2条回答
set方法时对 成员变量的封装双,成员变量设程私有的化,外面就访问不到了
.... setlive 方法只是返回个TRUE
啥也没干! .live = true 则是设置t的属性live的值为TRUE
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁关于NetBeans 6.5 的一个问题(什么叫“类 XX 是公共的,应在名为 XX .java 的文件中声明”)
[问题点数:0分]
关于NetBeans 6.5 的一个问题(什么叫“类 XX 是公共的,应在名为 XX .java 的文件中声明”)
[问题点数:0分]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
相关推荐:
本帖子已过去太久远了,不再提供回复功能。4860人阅读
&&&&& 题目有点长,不知道大家能不能明白!&&&&&&首先得出的结论是: &&&&&&&&&&&&& 它们是互相影响的,因为在一个实例类中同步方法锁定的是该实例类对象,因此会互相影响.&&&&&&& 下面是我做的一个测试:&&&&&&&&&&&&& (1)创建一个实例类:&&&&&&&&&&&&&&&&&&&&&&&&& package com.lenove.testT&&&&&&&&&&&&&&&&&&&&&&&&&& public class SynchronizationObject {&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& & public SynchronizationObject(){&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& & }& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&& public synchronized void runSynchronizationMethod0(){&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&& &&& System.out.println(&this is start test0!&);&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& //该循环的目地只有一个,就是可以使当前方法在一定时间内交出执行线程&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& for(int i = 0;i & ;i++){&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&& &&& System.out.println(&this is end test0!&);&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& }&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&& & public synchronized void runSynchronizationMethod1(){&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& & &&& System.out.println(&this is start test1!&);&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& for(int i = 0;i & ;i++){&&& &&& & &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&& System.out.println(&this is end test1!&);&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&& (2)创建一个调用的线程类&&&&&&&&&&&&&&&&&&&& package com.lenove.testT&&&&&&&&&&&&&&&&&&&& public class testThread implements Runnable {&&&&&&&&&&&&&&&&&&&&& &&& T&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& SynchronizationO&&&&&&&&&&&&&&&&&&&&&&&&&&&& public testThread(SynchronizationObject so,int type){&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&& &this.so =&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&& &th = new Thread(this);&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&& th.start();&&&&&&&&&&&&&&&&&&&&&&&&&&& &&& }&&&&&&&&&&&&&&&&&&&&&&&&&&&& &public void run() {&&&&&&&&&&&&&&&&&&&&&&&&&&& &&& &if(type == 1)&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&& so.runSynchronizationMethod1();&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&else &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&& &&so.runSynchronizationMethod0();&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&&&&&&& &public static void main(String[] args){&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&SynchronizationObject so = new SynchronizationObject();&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&new testThread(so,1);&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&new testThread(so,0);&&&&&&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&& }&&&&&&&&&& 程序执行后.无论执行多少次,结果为:&&&&&&&&&&&&&&&&&&&&&&&&& this is start test1!&&&&&&&&&&&&&&&&&&&&&&&&& this is end test1!&&&&&&&&&&&&&&&&&&&&&&&& this is start test0!&&&&&&&&&&&&&&&&&&&&&&&& this is end test0!&&&&&&&& 这时如果将SynchronizationObject 实例对象的任意一个方法的同步去掉(即去掉方法前面的synchronized),执行结果将出现一定的变化&&&第一次执行:&&&&&&&&&&&&&&&&&&&&&&&&&& 第二次执行:&&&&&& this is start test1!&&&&&&&&&&&&&&& this is start test1!& &&&&&& this is start test0!&&&&&&&&&&&&&&&& this is end test1!&&&&&& this is end test1!&&&&&&&&&&&&&&&&&& this is start test0!&&&&&& this is end test0!&&&&&&&&&&&&&&&&&& this is end test0!&&&&&&&&& 这时结果出现运行时刻输出结果不一致的情况,这说明同时访问一个实例对象的不同的同步方法,相互之间是有影响的,它们之间使用的锁就是该实例对象.&&&&&&&&& 运行情况分析:&&&&&&&&&&&&&&&&&& 当第一个线程执行时,运行到runSynchronizationMethod1()方法时,由于该方法是synchronizated,这时会先将so锁定,然后执行方法内的语句,中间由于大循环,线程交出执行权,交由第二个线程执行,第二个线程执行时,运行到runSynchronizationMethod0()方法,由于该方法也是synchronizated,这时该对象试图对so进行锁定,但由于之间该对象已被锁定,因此其锁定失败,需要等待第一个线程对so进行解锁,这里线程调度将控制权交给第一个线程执行,第一个线程执行完毕runSynchronizationMethod1()方法,这时对so进行解锁,这时线程交到第二个线程,第二个线程对so进行锁定操作,并可以锁定成功,因此继续执行完runSynchronizationMethod0()方法中的内容,.并进行解锁.
版权声明:本文为博主原创文章,未经博主允许不得转载。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:56885次
积分:1253
积分:1253
排名:第18538名
原创:75篇
评论:10条
(1)(4)(1)(1)(1)(2)(1)(1)(1)(1)(1)(1)(1)(2)(1)(1)(3)(1)(4)(3)(1)(1)(9)(1)(1)(1)(1)(3)(2)(1)(2)(1)(2)(1)(1)(1)(3)(6)(3)(2)String ret = Integer.toBinaryString(-5);
System.out.println(ret);
DecimalFormat df = new DecimalFormat
System.out.println(df.format(Integer.valueOf(ret)));
出现异常如下:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.valueOf(Unknown Source)
at com.develop.bit.TestBitCalculate.main(TestBitCalculate.java:60)
但如果把Integer.valueOf改成Double.valueOf,异常就没有了,但输出的值很奇怪。如下:
00,
求解!谢谢。
采纳的答案
主要是int/Integer是32位整型,按十进制看最大能表示的正整数只到
而Integer.valueOf(str)是将字符串以10进制方式解释,看看-5得到的binaryString:
显然要比int/Integer能表示的范围大,因而出错。
double/Double能表示的范围非常大,但是在绝对值很大或者很小的时候精度都会降低。仍然是因为Double.valueOf(str)也是将字符串以十进制解释,转换之后得到的是最接近给定字符串所指的数字的双精度浮点数,所以值与原本字符串所指定的不一定相同。
如果用Integer.valueOf(str, radix)的重载就可以指定转换时使用的进制。例如说把radix指定为2就可以指定解析表示二进制数的字符串:
Integer.valueOf("100"); // 4
但是用它来转换由Integer.toBinaryString(i)得到的字符串仍然是不可行的,因为这个方法得到的是无符号二进制数的字符串表示,而Integer.parseInt(str, radix)或者Integer.valueOf(str, radix)都接受的是带符号的数字的字符串表示。
可以参照Integer.parseInt(str, radix)的实现代码:
public static int parseInt(String s, int radix)
throws NumberFormatException
if (s == null) {
throw new NumberFormatException("null");
if (radix & Character.MIN_RADIX) {
throw new NumberFormatException("radix " + radix +
" less than Character.MIN_RADIX");
if (radix & Character.MAX_RADIX) {
throw new NumberFormatException("radix " + radix +
" greater than Character.MAX_RADIX");
int result = 0;
boolean negative =
int i = 0, max = s.length();
if (max & 0) {
if (s.charAt(0) == '-') {
negative =
limit = Integer.MIN_VALUE;
limit = -Integer.MAX_VALUE;
multmin = limit /
if (i & max) {
digit = Character.digit(s.charAt(i++),radix);
if (digit & 0) {
throw NumberFormatException.forInputString(s);
result = -
while (i & max) {
// Accumulating negatively avoids surprises near MAX_VALUE
digit = Character.digit(s.charAt(i++),radix);
if (digit & 0) {
throw NumberFormatException.forInputString(s);
if (result & multmin) {
throw NumberFormatException.forInputString(s);
if (result & limit + digit) {
throw NumberFormatException.forInputString(s);
throw NumberFormatException.forInputString(s);
if (negative) {
if (i & 1) {
} else { /* Only got "-" */
throw NumberFormatException.forInputString(s);
啊前面手滑了,有Integer.valueOf(str, radix)那个例子是:
Integer.valueOf("100", 2); // 4
已解决问题
未解决问题}

我要回帖

更多关于 java项目中遇到的问题 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信