当前位置:网站首页>构造器、方法重载、对象数组和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());
}
}
边栏推荐
- 将json文件中数组转换为struct
- 【socket】三次握手是在listen中完成,accept只从完成连接的队列中拿出一个连接
- [Halcon vision] morphological corrosion
- 函数模板与同名的非模板函数不可以重载(重载的定义)
- PTA class a 1002
- algorithm
- Mlx90640 infrared thermal imager temperature sensor module development notes (6)
- Google与Pixar开发Draco支持USD格式 加速3D对象传输&lt;转发&gt;
- Agenda express | list of sub forum agenda on July 27
- 并行、并发及对于高并发优化的几个方向
猜你喜欢

js下载文件,FileSaver.js导出txt、excel文件

2022/07/25 ------ arrangement of strings

js翻页、kkpager.js翻页
![[Halcon vision] threshold segmentation](/img/1c/e2463a796f99804a55680b69e714a6.png)
[Halcon vision] threshold segmentation
![[Halcon vision] morphological expansion](/img/ce/abaca036fce5b67dfe6ac361aecfea.png)
[Halcon vision] morphological expansion

码云,正式支持 Pages 功能,可以部署静态页面

【Halcon视觉】阈值分割
![[Halcon vision] Fourier transform of image](/img/9c/d6ed4ab3e40f706f3b5b8b5cc51db9.png)
[Halcon vision] Fourier transform of image

Employee information management system based on Web

【Halcon视觉】图像灰度变化
随机推荐
mysql 进不去了怎么办
一些你不知道的 web API
Self encapsulated database dbutils universal template
【Halcon视觉】极坐标变换
js,e.pageX、pageY模态框拖动
C语言计算日期间隔天数
【Halcon视觉】数组
Google与Pixar开发Draco支持USD格式 加速3D对象传输&lt;转发&gt;
Mlx90640 infrared thermal imager temperature sensor module development notes (6)
The software cannot be opened
函数模板与同名的非模板函数不可以重载(重载的定义)
[Halcon vision] morphological corrosion
抓包工具fiddler和wireshark对比
Modelsim installation tutorial (application not installed)
Comparison of packet capturing tools fiddler and Wireshark
PLC overview
Introduction to data analysis | kaggle Titanic mission
[Halcon vision] polar coordinate transformation
分布式锁解决方案之Redis实现
详细解析js中的混合方式构造对象(构造加属性,原型加方法)