当前位置:网站首页>构造器、方法重载、对象数组和static
构造器、方法重载、对象数组和static
2022-07-26 10:31:00 【一尾流鸢cd】
1、构造器
- 构造器的由来:为了防止在给对象输入值时漏掉一些属性
- 概念:构造器也叫构造方法,是一种特殊的方法,此方法中方法名与类名相同
- 作用:为了初始化对象,给予对象开始时自己想要的值。
- 特点:
- 通过new关键字被调,方法名与类名相同。在创建对象调用构造器时,如果想让构造出的对象有一定的属性则可以直接书写
- 当类中无自定义构造器时,系统提供的默认的构造器;有自定义构造器时,系统提供的构造器就消失了。
- 在本类中,如果想要调用其他构造器,则可以用this(实际参数);调用。
注意:this不能来回调用,且必须写在调用所在地的首句。 - 构造器可以重载
- 何时该自定义构造器?
当实例化对象想改变默认的初始值时,可以使用。
public class Student{
/* 此代码,构造器调用时,先执行主方法中的Student stu=new Student("张三"); 其次根据new Student("张三");调用Student(String name){}构造器,在构造器中先执行this("王五",19); ,通过this关键字调用Student(String name,int age){},在这个构造器中name没有输出所以先输出的是:hello;再返回到Student(String name){}执行其中的语句输出:张三 */
String name;
int age;
Student(String name){
//构造方法名与类名相同。此处有自定义构造器 ,则系统默认构造器消失
this("王五",19);//此处使用this关键字调用Student类中的其他构造器
//this关键字必须写在调用所在地的首句。
this.name=name;
System.out.println(name);
}
Student(String name,int age){
//构造器可以重载,名相同,参数列表不同
//this("李三"); 如果此处放开的话会报错,this不能来回调用
System.out.println("hello");
}
public static void main(String[] args) {
Student stu=new Student("张三");//构造方法通过new关键字被调
}
}
2、方法重载
- 概念:在同一个内中,方法名相同,参数列表不同(个数,类型,顺序),与返回值的类型无关
- 好处:减轻对方法名的记忆
public class getPrice(){
//在同一个类中,方法名相同,参数列表不同。(个数,类型,顺序)。
public void getPrice(int a){
}
public void getPrice(int a,int b){
}
// public void getPrice(int c,int d){}//参数列表类型和数量都相同
public void getPrice(char c,int a){
}
public String getPrice(int a,char d){
return "1";}
// public void getPrice(int r){}//不是,参数列表类型相同
//public float getPrice(int f){return 1;}//不是,参数列表类型相同
public char getPrice(int a,int b,int c){
return 'a';}//不是
public void getPrice(int a,float b){
}//
public void getPrice(int a,String b,char c){
}
public void getPrice(String g,int a){
}
public String getPrice(String s){
return "";}
public String getPrice(float f,String n){
return "";}
注意:返回值与是否属于方法重载无关!
}
3、对象数组
在数组中存储一组对象,有一个长度的限制!!
- 定义方式:类名 [] 数组名=new 类名[数组长度];
类名 数组名[]=new 类名[数组长度];
类名 [] 数组名={}; 等等
注意:定义方式与数组的定义方式类似 - 使用方式:
public class Computer {
String name;
int price;
Computer(String name, int price) {
this.name = name;
this.price = price;
}
public void print() {
System.out.println("电脑品牌:" + name + " 电脑价格:" + price);
}
public static void main(String[] args) {
Computer arrCom[] = new Computer[3];//创建对象数组
arrCom[0] = new Computer("lenovo", 7000);
arrCom[1] = new Computer("华硕", 3000);
arrCom[2] = new Computer("小米", 5000);
// for (Computer computer : arrCom) {
// computer.print();
// }
for(int i=0;i<arrCom.length;i++){
arrCom[i].print();//调用每个数组对象的print();
}
}
}
4、static(简单介绍)
- 静态变量:被static修饰的变量,也叫类变量。通过类名.属性名可以直接访问。也可以实例化对象,通过对象名.属性名访问
- 静态方法:static修饰的方法,也叫类方法。通过类名.方法名可以直接访问。也可以实例化对象,通过对象名.方法名访问
- 静态代码块:随着类的加载而加载,随着类的消失而消失,而且只运行一次,且优先于对象执行
- 静态方法只能访问静态方法,实例方法(普通方法)既可以访问静态方法也可以访问实例方法。
5、构造器和静态的使用
当属性和所有(创建的对象)成员有关时,使用static(公共的属性)
public class Person {
String name;
static int rs;
Person(){
this.rs++;
}
public static int getToPrice(){
return rs*6000;
}
public static void main(String[] args) {
Person person1=new Person();
System.out.println("当前人数为:"+Person.rs);
Person person2=new Person();
System.out.println("当前人数为:"+Person.rs);
Person person3=new Person();
System.out.println("当前人数为:"+Person.rs);
Person person4=new Person();
System.out.println("当前人数为:"+Person.rs);
System.out.println("总学费:"+Person.getToPrice());
}
}
边栏推荐
- 2022pta usual training questions (1-10 string processing questions)
- Closure of go (cumulative sum)
- 【Halcon视觉】算子的结构
- Inheritance method of simplified constructor (II) - class inheritance in ES6
- algorithm
- C语言回调函数
- json_object_put: Assertion `jso->_ref_count > 0‘ failed.Aborted (core dumped)
- 【Halcon视觉】图像滤波
- 简单化构造函数的继承方法(二)- ES6中的class继承
- 我们的Web3创业项目,黄了
猜你喜欢
![[Halcon vision] affine transformation](/img/f1/32284c71e78e6eea390fdb6058ba0f.png)
[Halcon vision] affine transformation

2022/07/25 ------ arrangement of strings
![[Halcon vision] programming logic](/img/1a/b6daac946fbefd8337355dc8b7873e.png)
[Halcon vision] programming logic

我们的Web3创业项目,黄了

INSTALL_FAILED_SHARED_USER_INCOMPATIBLE错误解决方式

js 获得当前时间,时间与时间戳的转换

Learning about opencv (3)

3.1 leetcode daily question 6

Unit test, what is unit test and why is it so difficult to write a single test

Dynamically determine file types through links
随机推荐
.NET操作Redis Hash对象
Some cutting-edge research work sharing of SAP ABAP NetWeaver containerization
js,e.pageX、pageY模态框拖动
Learning about tensorflow (I)
【Halcon视觉】极坐标变换
Some descriptions of DS V2 push down in spark
微信公众号发布提醒(微信公众号模板消息接口)
Review of database -- 3. SQL language
2022pta usual training questions (1-10 string processing questions)
并行、并发及对于高并发优化的几个方向
Agenda express | list of sub forum agenda on July 27
将json文件中数组转换为struct
Mlx90640 infrared thermal imager temperature sensor module development notes (6)
句句解析js中的完美 / 缓冲运动框架(新手专用)
Structure of [Halcon vision] operator
Our Web3 entrepreneurship project is yellow
[socket] the three handshakes are completed in listen, and accept only takes out one connection from the queue that completes the connection
面试第一家公司的面试题及答案(一)
.NET操作Redis String字符串
js翻页、kkpager.js翻页