Java之.this与.new的使用
时间: 2023-11-27 08:49:10 浏览: 35
Java 中的 .this 关键字用于引用当前对象,可以在对象内部使用,以明确指定需要访问的实例变量或方法。例如:
```
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public void sayHello() {
System.out.println("Hello, my name is " + this.name);
}
}
```
在上面的例子中,this.name 引用了当前对象的 name 实例变量。
而 .new 关键字用于创建一个新的对象实例。例如:
```
Person person = new Person("Tom");
```
这里使用 .new 创建了一个新的 Person 对象实例,并将其赋值给 person 变量。
相关问题
java里面that.跟this.的区别
在Java中,"this"和"that"都是关键字,用于引用对象或类的成员变量、方法和构造函数。它们之间的区别在于:
1. "this"关键字只能在类的内部使用,它指的是当前对象的引用。可以使用它来访问当前对象的属性或调用当前对象的方法。
2. "that"关键字可以在类的内部和外部使用,它指的是其他对象的引用。通常使用它来引用其他对象的属性或方法。
例如,下面的代码演示了"this"关键字的用法:
```
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name; // 使用this关键字访问当前对象的属性
this.age = age;
}
public void introduce() {
System.out.println("My name is " + this.name + ", and I'm " + this.age + " years old."); // 使用this关键字调用当前对象的方法
}
}
```
而下面的代码演示了"that"关键字的用法:
```
public class Main {
public static void main(String[] args) {
Person p1 = new Person("Tom", 18);
Person p2 = new Person("Jack", 20);
System.out.println(p1.name); // 直接访问对象的属性
System.out.println(p2.getName()); // 使用对象的方法访问属性
System.out.println(that.name); // 错误!无法访问“that”关键字
}
}
```
java.util.concurrent.ThreadPoolExecutor 测试 main 使用submit
以下是使用java.util.concurrent.ThreadPoolExecutor测试main方法使用submit的示例:
```java
import java.util.concurrent.*;
public class ThreadPoolExecutorTest {
public static void main(String[] args) {
int corePoolSize = 5;
int maxPoolSize = 10;
long keepAliveTime = 5000;
BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(100);
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, TimeUnit.MILLISECONDS, workQueue);
for (int i = 0; i < 20; i++) {
threadPoolExecutor.submit(new Task(i));
}
threadPoolExecutor.shutdown();
}
static class Task implements Runnable {
private int taskId;
public Task(int taskId) {
this.taskId = taskId;
}
@Override
public void run() {
System.out.println("Task #" + this.taskId + " is running.");
}
}
}
```
在这个示例中,我们创建了一个ThreadPoolExecutor对象,并使用submit方法向线程池提交了20个任务。每个任务都是一个简单的Runnable对象,它只是打印一条消息。
注意,我们在最后调用了shutdown方法来关闭线程池。这是非常重要的,因为如果不关闭线程池,程序将一直运行下去。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)