当前位置:网站首页>构造器、方法重载、对象数组和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());
}
}
边栏推荐
猜你喜欢

Learning about opencv (4)

抓包工具fiddler和wireshark对比

【Halcon视觉】形态学膨胀

【Halcon视觉】图像灰度变化

我们的Web3创业项目,黄了
The software cannot be opened
![[Halcon vision] array](/img/29/905d93795a24538fded18d2d377e52.png)
[Halcon vision] array

Agenda express | list of sub forum agenda on July 27

Deduct daily question 838 of a certain day

Introduction to data analysis | kaggle Titanic mission (I) - > data loading and preliminary observation
随机推荐
Wechat official account release reminder (wechat official account template message interface)
句句解析js中的完美 / 缓冲运动框架(新手专用)
Navicat15连接本地虚拟机的Mysql(Centos7)
Introduction to Phoenix (Level 1: Phoenix installation, level 2: Phoenix basic grammar)
头歌 Phoenix 入门(第1关:Phoenix 安装、第2关:Phoenix 基础语法)
数据分析入门 | kaggle泰坦尼克任务
[Halcon vision] polar coordinate transformation
The software cannot be opened
关于模板函数声明与定义的问题[通俗易懂]
canvas上传图片base64-有裁剪功能-Jcrop.js
2022/07/25------字符串的排列
Our Web3 entrepreneurship project is yellow
Some descriptions of DS V2 push down in spark
PLC overview
algorithm
Android greendao数据库的使用
What is wrong about the description of function templates (how to solve link format errors)
函数模板参数(函数参数在哪)
The reason why go language is particularly slow to develop run and build commands
Some cutting-edge research work sharing of SAP ABAP NetWeaver containerization