Java-类-方法
在本教程中,我们将学习Java编程语言中的类的方法。
在上一个教程"类-成员变量"中,我们学习了如何在类内部创建成员变量,并且还介绍了控制类变量可访问性的访问修饰符。
现在,让我们讨论对类的成员变量进行操作的方法。
方法语法
以下是Java中方法声明的语法。
access_modifier return_type method_name(list_of_parameters) {
//body of the method
}
其中,access_modifier是一些访问修饰符,用于定义方法的可访问性。
return_type定义了方法返回的数据类型。
" method_name"是为方法指定的有效名称。
list_of_parameters是一个参数列表,它告诉我们该方法可以接受的参数。
该方法的主体从{打开大括号开始,以}结束大括号结束。
命名方式
以下是命名方法时需要牢记的几点。
命名方法时,请使用a-z,A-Z,0-9和下划线_字符。
使用简单的描述性名称。
方法名称不能以数字开头。
除非必要且合理,否则请避免使用缩写词。
避免使用诸如
for,if等关键字。
以下是一些有效的方法名称。
findAverage computeVolume printResult add_user_to_team
以下是无效的方法名称。
//can't start with digit 1group //character - is not allowed in method name super-user //can't use space Sample UserDetail
返回方法类型
方法的返回类型告诉我们该方法返回的数据类型。
返回类型可以是Java的任何基本数据类型,例如char,int,double等。
如果该方法不返回任何值,则将返回类型设置为" void"。
方法的访问修饰符
访问修饰符定义方法的可访问性,以下是我们可以添加到方法中的访问修饰符。
上市
如果我们将方法的访问修饰符设置为" public",则可以从类的内部和外部访问它。
私人的
当我们将方法的访问修饰符设置为"私有"时,则只能从类内部访问它。
受保护的
" protected"访问修饰符使方法可以在类内部以及继承该方法的类中访问。
继承教程中有关继承的更多信息。
this关键字
我们在类内部使用关键字" this"来访问该类的成员。
因此,例如,如果我们要从方法" getLength()"内部访问成员变量" length",则可以编写以下" this.length",它将为我们提供" PackagingBox"类的长度。
向PackagingBox类添加方法
在"类简介"教程中,我们创建了" PackagingBox"类,并在上一教程中添加了一些成员变量。
因此,我们的类代码如下所示。
class PackagingClass {
//member variables
private double length;
private double breadth;
private double height;
public double volume;
double weight;
double price;
}
现在,让我们向类添加一些方法。
示例:创建getter和setter方法
要获取并设置length,breadth和height变量的值,我们可以编写以下方法。
public void setLength(double length) {
this.length = length;
}
public double getLength() {
return this.length;
}
public void setBreadth(double breadth) {
this.breadth = breadth;
}
public double getBreadth() {
return this.breadth;
}
public void setHeight(double height) {
this.height = height;
}
public double getHeight() {
return this.height;
}
注意!在set方法中,我们传递了一个参数,然后将其分配给成员变量。
同样,我们可以为weight和price变量创建get和set方法。
public void setWeight(double weight) {
this.weight = weight;
}
public double getWeight() {
return this.weight;
}
public void setPrice(double price) {
this.price = price;
}
public double getPrice() {
return this.price;
}
对于" volume",我们可以创建一个将计算盒子体积的方法,以及另一个将返回体积的方法。
public void computeVolume() {
this.volume = this.length * this.breadth * this.height;
}
public double getVolume() {
return this.volume;
}
该类的完整代码如下所示。
class PackagingClass {
//member variables
private double length;
private double breadth;
private double height;
public double volume;
double weight;
double price;
//methods
//---- get and set length
public void setLength(double length) {
this.length = length;
}
public double getLength() {
return this.length;
}
//---- get and set breadth
public void setBreadth(double breadth) {
this.breadth = breadth;
}
public double getBreadth() {
return this.breadth;
}
//---- get and set height
public void setHeight(double height) {
this.height = height;
}
public double getHeight() {
return this.height;
}
//---- get and set weight
public void setWeight(double weight) {
this.weight = weight;
}
public double getWeight() {
return this.weight;
}
//---- get and set price
public void setPrice(double price) {
this.price = price;
}
public double getPrice() {
return this.price;
}
//---- compute and get volume
public void computeVolume() {
this.volume = this.length * this.breadth * this.height;
}
public double getVolume() {
return this.volume;
}
}

