Java 简明教程
Java - Class Methods
Java Class Methods
class methods 是在 class 中声明的方法。它们执行特定操作,并且可以访问和修改 class attributes。
The class methods are methods that are declared within a class. They perform specific operations and can access, modify the class attributes.
Creating (Declaring) Java Class Methods
类方法声明类似于 user-defined methods 声明,不同之处在于类方法是在类中声明的。
Class methods declaration is similar to the user-defined methods declaration except that class methods are declared within a class.
类方法通过指定 access modifier 后跟返回类型、method_name 和参数列表来声明。
The class methods are declared by specifying the access modifier followed by the return type, method_name, and parameters list.
Syntax
使用以下语法声明 Java 类方法:
Use the below syntax to declare a Java class method:
public class class_name {
modifier returnType nameOfMethod(Parameter List) {
// method body
}
}
上面显示的语法包括:
The syntax shown above includes −
-
modifier − It defines the access type of the method and it is optional to use.
-
returnType − The returns data type of the class method.
-
nameOfMethod − This is the method name. The method signature consists of the method name and the parameter list.
-
Parameter List − The list of parameters, it is the type, order, and number of parameters of a method. These are optional, method may contain zero parameters.
-
method body − The method body defines what the method does with the statements.
Example
下面是上面定义的方法的源代码,名为 minimum()。此方法需要两个参数 n1 和 n2,并返回两者的最小值 −
Here is the source code of the above defined method called minimum(). This method takes two parameters n1 and n2 and returns the minimum between the two −
class Util {
/** the snippet returns the minimum between two numbers */
public int minimum(int n1, int n2) {
int min;
if (n1 > n2)
min = n2;
else
min = n1;
return min;
}
}
Accessing Java Class Methods
要访问一个类方法(公共类方法),首先需要创建一个对象,然后使用该对象,并借助圆点 (.) 算子,您可以访问类方法。
To access a class method (public class method), you need to create an object first, then by using the object you can access the class method (with the help of dot (.) operator).
Syntax
使用下面的语法访问 Java 公共类方法:
Use the below syntax to access a Java public class method:
object_name.method_name([parameters]);
Example
下面是一个示例,演示如何定义类方法和访问类方法。在此,我们创建了一个 Util 类对象,并调用其 minimum() 方法,以获得给定两个数字的最小值 −
Following is the example to demonstrate how to define class method and how to access it. Here, We’ve created an object of Util class and call its minimum() method to get minimum value of given two numbers −
package com.tutorialspoint;
class Util {
public int minimum(int n1, int n2) {
int min;
if (n1 > n2)
min = n2;
else
min = n1;
return min;
}
}
public class Tester {
public static void main(String[] args) {
int a = 11;
int b = 6;
Util util = new Util();
int c = util.minimum(a, b);
System.out.println("Minimum Value = " + c);
}
}
Minimum value = 6
this Keyword in Java Class Methods
this 是 Java 中的一个关键字,用作当前类对象的引用,它存在于实例方法或构造函数中。通过使用此关键字,您可以引用类的成员,例如构造函数、变量和方法。
this is a keyword in Java which is used as a reference to the object of the current class, with in an instance method or a constructor. Using this you can refer the members of a class such as constructors, variables and methods.
Note− 关键字 this 只用在实例方法或构造函数内
Note − The keyword this is used only within instance methods or constructors
一般来说,关键字 this 用于 −
In general, the keyword this is used to −
-
Differentiate the instance variables from local variables if they have same names, within a constructor or a method.
class Student {
int age;
Student(int age) {
this.age = age;
}
}
-
Call one type of constructor (parametrized constructor or default) from other in a class. It is known as explicit constructor invocation.
class Student {
int age
Student() {
this(20);
}
Student(int age) {
this.age = age;
}
}
Example: Using this Keyword in Java Class Methods
这是一个使用 this 关键字访问类成员的示例。复制并粘贴以下程序到名为 Tester.java 的文件中。
Here is an example that uses this keyword to access the members of a class. Copy and paste the following program in a file with the name, Tester.java.
package com.tutorialspoint;
public class Tester {
// Instance variable num
int num = 10;
Tester() {
System.out.println("This is an example program on keyword this");
}
Tester(int num) {
// Invoking the default constructor
this();
// Assigning the local variable num to the instance variable num
this.num = num;
}
public void greet() {
System.out.println("Hi Welcome to Tutorialspoint");
}
public void print() {
// Local variable num
int num = 20;
// Printing the local variable
System.out.println("value of local variable num is : "+num);
// Printing the instance variable
System.out.println("value of instance variable num is : "+this.num);
// Invoking the greet method of a class
this.greet();
}
public static void main(String[] args) {
// Instantiating the class
Tester obj1 = new Tester();
// Invoking the print method
obj1.print();
// Passing a new value to the num variable through parametrized constructor
Tester obj2 = new Tester(30);
// Invoking the print method again
obj2.print();
}
}
This is an example program on keyword this
value of local variable num is : 20
value of instance variable num is : 10
Hi Welcome to Tutorialspoint
This is an example program on keyword this
value of local variable num is : 20
value of instance variable num is : 30
Hi Welcome to Tutorialspoint
Public Vs. Static Class Methods
有两种类型的类方法,public 类方法和 static 类方法。public class methods 通过对象访问,而 static class methods 直接访问。您可以直接访问静态方法。
There are two types of class methods public and static class method. The public class methods are accessed through the objects whereas, the static class methods are accessed are accesses without an object. You can directly access the static methods.
Example
以下示例演示了公共类方法和静态类方法之间的区别:
The following example demonstrates the difference between public and static class methods:
public class Main {
// Creating a static method
static void fun1() {
System.out.println("fun1: This is a static method.");
}
// Creating a public method
public void fun2() {
System.out.println("fun2: This is a public method.");
}
// The main() method
public static void main(String[] args) {
// Accessing static method through the class
fun1();
// Creating an object of the Main class
Main obj = new Main();
// Accessing public method through the object
obj.fun2();
}
}
fun1: This is a static method.
fun2: This is a public method.
The finalize( ) Method
可以定义一种方法,在垃圾收集器对某个对象进行最终销毁之前,将会调用该方法。此方法称为 finalize( ),可用于确保对象干净地终止。
It is possible to define a method that will be called just before an object’s final destruction by the garbage collector. This method is called finalize( ), and it can be used to ensure that an object terminates cleanly.
例如,您可以使用 finalize( ) 确保由该对象拥有的打开文件已关闭。
For example, you might use finalize( ) to make sure that an open file owned by that object is closed.
要为类添加一个终结器,只需定义 finalize( ) 方法。每当 Java 运行时准备回收该类的对象时,就会调用该方法。
To add a finalizer to a class, you simply define the finalize( ) method. The Java runtime calls that method whenever it is about to recycle an object of that class.
在 finalize( ) 方法中,您将指定在销毁对象之前必须执行的操作。
Inside the finalize( ) method, you will specify those actions that must be performed before an object is destroyed.
finalize( ) 方法具有以下常规形式 −
The finalize( ) method has this general form −
protected void finalize( ) {
// finalization code here
}
此处,关键字 protected 是一个说明符,该说明符阻止定义在其类外部的代码访问 finalize( )。
Here, the keyword protected is a specifier that prevents access to finalize( ) by code defined outside its class.
这意味着你无法知道finalize()何时或是否会被执行。例如,如果你的程序在垃圾收集发生之前结束,则finalize()将不会执行。
This means that you cannot know when or even if finalize( ) will be executed. For example, if your program ends before garbage collection occurs, finalize( ) will not execute.