this and super keyword in java

tinyfrog519

New member
#Java #superkeyword #thiskeyword #Keyword #Javatutorial ## từ khóa này và siêu khóa trong java

Các từ khóa `this` và` Super` là hai trong số các từ khóa quan trọng nhất trong Java.Chúng được sử dụng để tham khảo đối tượng hiện tại và lớp cha của đối tượng hiện tại, tương ứng.

Từ khóa `` này có thể được sử dụng để tham khảo đối tượng hiện tại trong một phương thức hoặc hàm tạo.Điều này có thể hữu ích để truy cập các trường của đối tượng hoặc gọi các phương thức của nó.Ví dụ: mã sau in giá trị của trường `name` của đối tượng hiện tại:

`` `java
người lớp công khai {
Tên chuỗi riêng;

người công cộng (tên chuỗi) {
this.name = name;
}

void void setName (tên chuỗi) {
this.name = name;
}

chuỗi công khai getName () {
trả lại cái này .name;
}
}

lớp công khai chính {
công khai void void main (String [] args) {
Người người = người mới ("John Doe");
System.out.println (person.getName ());// In "John Doe"
}
}
`` `

Từ khóa `Super` có thể được sử dụng để tham khảo lớp cha của lớp hiện tại.Điều này có thể hữu ích cho việc gọi các hàm tạo, phương thức hoặc trường của lớp cha.Ví dụ: mã sau in giá trị của trường `super.name` của đối tượng hiện tại:

`` `java
người lớp công cộng mở rộng động vật {
Tên chuỗi riêng;

người công cộng (tên chuỗi) {
Siêu (tên);
}

void void setName (tên chuỗi) {
this.name = name;
}

chuỗi công khai getName () {
trả lại cái này .name;
}
}

lớp công khai chính {
công khai void void main (String [] args) {
Người người = người mới ("John Doe");
System.out.println (person.getName ());// In "John Doe"
}
}
`` `

Từ khóa `` này và `Super` là các công cụ mạnh mẽ có thể được sử dụng để cải thiện khả năng đọc và khả năng bảo trì của mã Java của bạn.Bằng cách hiểu cách sử dụng các từ khóa này, bạn có thể viết mã hiệu quả và hiệu quả hơn.

## hashtags

* Java
* Từ khóa siêu
* Từ khóa này
* Từ khóa
* Hướng dẫn Java
=======================================
#Java #superkeyword #thiskeyword #Keyword #Javatutorial ##This and super keyword in Java

The `this` and `super` keywords are two of the most important keywords in Java. They are used to refer to the current object and the parent class of the current object, respectively.

The `this` keyword can be used to refer to the current object in a method or constructor. This can be useful for accessing the object's fields or calling its methods. For example, the following code prints the value of the `name` field of the current object:

```java
public class Person {
private String name;

public Person(String name) {
this.name = name;
}

public void setName(String name) {
this.name = name;
}

public String getName() {
return this.name;
}
}

public class Main {
public static void main(String[] args) {
Person person = new Person("John Doe");
System.out.println(person.getName()); // prints "John Doe"
}
}
```

The `super` keyword can be used to refer to the parent class of the current class. This can be useful for calling the parent class's constructors, methods, or fields. For example, the following code prints the value of the `super.name` field of the current object:

```java
public class Person extends Animal {
private String name;

public Person(String name) {
super(name);
}

public void setName(String name) {
this.name = name;
}

public String getName() {
return this.name;
}
}

public class Main {
public static void main(String[] args) {
Person person = new Person("John Doe");
System.out.println(person.getName()); // prints "John Doe"
}
}
```

The `this` and `super` keywords are powerful tools that can be used to improve the readability and maintainability of your Java code. By understanding how to use these keywords, you can write more efficient and effective code.

## Hashtags

* Java
* Super keyword
* This keyword
* Keyword
* Java tutorial
 
Join ToolsKiemTrieuDoGroup
Back
Top
AdBlock Detected

We get it, advertisements are annoying!

Sure, ad-blocking software does a great job at blocking ads, but it also blocks useful features of our website. For the best site experience please disable your AdBlocker.

I've Disabled AdBlock