private String num="12313"
时间: 2023-07-06 21:23:19 浏览: 44
在Java中,使用`private`关键字修饰类的属性时,表示这个属性只能在当前类中被访问和修改,其他类无法直接访问。如果需要访问和修改这个属性的值,可以通过提供公共的访问方法(getter和setter方法)来实现。
对于`private String num = "12313"`这个语句,它定义了一个私有属性`num`,并且为它赋了初始值"12313"。其他类无法直接访问这个属性,只有在当前类的方法中才能访问和修改它的值。
如果需要在其他类中访问和修改这个属性的值,可以提供一个公共的访问方法,例如:
```
public class MyClass {
private String num = "12313";
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
}
```
在上面的例子中,提供了公共的getter和setter方法来访问和修改私有属性`num`的值。其他类可以通过这两个方法来间接地访问和修改`num`的值。
相关问题
.下面的程序定义了一个线程类,请填空把程序补充完整。 public class MyThread implements (1) { private int num = 0; private boolean stop = false; public static void main(String[] args) { MyThread my = new MyThread(); Thread td = new Thread(my);
public class MyThread implements Runnable {
private int num = 0;
private boolean stop = false;
public void run() {
while(!stop) {
System.out.println("num = " + num);
num++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
MyThread my = new MyThread();
Thread td = new Thread(my);
td.start();
}
}
public class PhoneUtil { public static final String NAME = "display_name"; public static final String NUM = "data1"; private Context context; private Uri phoneUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; public PhoneUtil(Context context) { this.context = context; } public List<PhoneDto> getPhone() { ArrayList arrayList = new ArrayList(); Cursor query = this.context.getContentResolver().query(this.phoneUri, new String[]{NUM, NAME}, null, null, null); while (query.moveToNext()) { arrayList.add(new PhoneDto(query.getString(query.getColumnIndex(NAME)), query.getString(query.getColumnIndex(NUM)))); } return arrayList; } }
这是一个获取手机通讯录中所有联系人姓名和电话号码的工具类,使用了Android系统提供的ContactsContract API。在构造函数中传入Context对象,通过getContentResolver()方法获取ContentResolver对象,然后调用query()方法查询手机通讯录中的联系人信息,将结果封装成PhoneDto对象并添加到ArrayList中,最终返回ArrayList。其中PhoneDto是一个自定义的类,用于封装联系人姓名和电话号码。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)