当前位置:网站首页>Constructor, this keyword, method overloading, local variables and member variables
Constructor, this keyword, method overloading, local variables and member variables
2022-08-02 00:32:00 【Weak crown original intention】
1.构造方法的作用:初始化对象
特点:1),无返回值 2),方法名和类名相同 3),可以指定参数
访问修饰符 构造方法名 ( ) {
//初始化代码
}
2.this 关键字的用法:
1),调用属性
this.name = "大黄"
2),调用方法
this.print();
3),调用构造方法:如果使用,Must be placed in the first statement of the constructor
this();
thisKeywords can be used to differentiate
3.方法的重载:在同一个类中,Multiple methods appear with the same method name,参数列表不同(参数的个数、参数的类型、参数顺序)的现象.
- 同一个类中
- 方法名相同
- 参数个数和类型不同
- 与返回值和访问修饰符无关
public class Sum {
//Define several common methods
public int sum(int num1,int num2){
return num1+num2;
}
public double sum(double num1,double num2){
return num1+num2;
}
public double sum(int num1,double num2){
return num1+num2;
}}
4.成员变量和局部变量
变量作用域:Variables follow their location,Can be divided into member variables(全局变量)、There are two types of local variables
成员变量(类内方法外):
Variables outside other structures in the acting class,
The scope of member variables is available throughout the class(Non-static member variables cannot be used in static methods,Static member variables can be used)
The member variable system assigns it a default value
在同一个类中,There cannot be a global variable with the same name,全局变量和局部变量可以同名,在使用的时候,局部变量具有更高的优先级
public class Demo01 {
String name;
int num1 = 1000;//成员变量
public void test(){
int num1;
System.out.println(name);
num1 = 100;
//Local variables must be assigned a value before being used
System.out.println(num1);
}
局部变量(方法内):
Variables in action methods or other structures,
The scope of a local variable is limited to the structure in which the local variable is defined
局部变量没有默认值,Assign values before use,否则会报错
in different methods(Get inside other structures)可以有相同名称的局部变量,There cannot be local variables with the same name within the same method or structure
public class Demo01 {
String name;
int num1 = 1000;//成员变量
public void test(){
int num1;//方法内的局部变量
System.out.println(name);
num1 = 100;
//Local variables must be assigned a value before being used
System.out.println(num1);
}
边栏推荐
猜你喜欢

短视频seo搜索优化主要内容

不就是个TCC分布式事务,有那么难吗?

如何设计循环队列?快进来学习~

22. The support vector machine (SVM), gaussian kernel function

Axure tutorial - the new base (small white strongly recommended!!!)

Task execution control in Ansible

短视频SEO搜索运营获客系统功能介绍

An overview of the most useful DeFi tools

认识USB、Type-C、闪电、雷电接口

146. LRU cache
随机推荐
BGP 第一次实验
玩转NFT夏季:这份工具宝典值得收藏
单片机遥控开关系统设计(结构原理、电路、程序)
JSP out.print()和out.write()方法的不同之处
Graphical LeetCode - 1161. Maximum Sum of In-Layer Elements (Difficulty: Moderate)
不了解SynchronousQueue?那ArrayBlockingQueue和LinkedBlockingQueue不会也不知道吧?
Difference between JSP out.print() and out.write() methods
JSP built-in object out object function introduction
短视频seo搜索优化主要内容
[头条]笔试题——最小栈
Knowing the inorder traversal of the array and the preorder traversal of the array, return the postorder history array
Short video SEO search operation customer acquisition system function introduction
已知中序遍历数组和先序遍历数组,返回后序遗历数组
Statement执行update语句
els 方块变形判断。
基于相关性变量筛选偏最小二乘回归的多维相关时间序列建模方法
[Solution] Emqx startup under win10 reports Unable to load emulator DLL, node.db_role = EMQX_NODE__DB_ROLE = core
ROS 动态参数
控制电机的几种控制电路原理图
重装腾讯云云监控后如果对应服务不存在可通过sc.exe命令添加服务