当前位置:网站首页>构造方法、方法重载、全局变量与局部变量
构造方法、方法重载、全局变量与局部变量
2022-08-02 03:11:00 【抬眼远望】
构造方法
构造方法是类中的一种特殊方法,当程序用类创建对象时需使用它的构造方法。
语法规则:访问权限修饰符 类名(参数列表){ 方法体 }
类中的构造方法的名字必须与它所在的类的名字完全相同,而且没有类型。允许在一个类中编写若干个构造方法,但必须保证它们的参数不同,参数不同是指:参数的个数不同,或参数个数相同,但参数列表中对应的某个参数的类型不同。
需要注意的是,如果类中没有编写构造方法,系统会默认该类只有一个构造方法,该默认的构造方法是无参数的,且方法体重没有语句。
public class Student {
// 定义属性
String name;
int age;
char gender;
double height;
double weight;
String address;
double score;
long phoneNumber;
// Student类中有一个默认的无参构造函数,只是隐藏起来了,不显示而已
/*
* 访问权限修饰符 类名(参数列表){ 方法体 }
*/
// 声明一个有参构造函数来创建对象
public Student() {
}
public Student(String name, int age, char gender, double height, double weight, String address,
double score, long phoneNumber) {
//this表示当前调用此方法的对象,谁调用这个方法this就表示谁
this.name = name;
this.age = age;
this.gender = gender;
this.height = height;
this.weight = weight;
this.address = address;
this.score = score;
this.phoneNumber = phoneNumber;
}
// 声明一个方法来输出对象的所有信息
public void printInfo() {
System.out.println("姓名:" + this.name + ",年龄:" + this.age + ",性别:" + this.gender
+ ",身高:" + this.height + ",体重:" + this.weight + ",地址:" + this.address + ",分数:"
+ this.score + ",电话:" + this.phoneNumber);
}
}
public class StudentTest {
public static void main(String[] args) {
//使用Student类里的无参构造函数创建对象
Student stu = new Student();
//给对象属性赋值
stu.name = "张三";
stu.age = 22;
stu.gender = '男';
stu.height = 170;
stu.weight = 140;
stu.address = "安徽合肥";
stu.phoneNumber = 13012341234L;
stu.score = 99.5;
stu.printInfo();
//上面对象的属性是一个一个进行赋值,比较麻烦,能不能在创建对象的时候一次性赋值?-->通过有参构造函数来实现
Student stu2 = new Student("李四", 21, '男', 175.5, 135.8, "安徽安庆", 98.5, 13878784646L);
stu2.printInfo();
}
}
public class Teacher {
String name;
int age;
char gender;
double height;
double weight;
String address;
double score;
long phoneNumber;
//添加无参构造方法
public Teacher() {
}
//添加有参构造方法
public Teacher(String name, int age, char gender, double height,
double weight, String address, double score, long phoneNumber) {
this.name = name;
this.age = age;
this.gender = gender;
this.height = height;
this.weight = weight;
this.address = address;
this.score = score;
this.phoneNumber = phoneNumber;
}
}
public class Penguin {
String name;
int health;
char sex;
//注意:下面这个方法不是构造方法,只是方法名没有按照规范命名,方法名正好是类名而已
public void Penguin(){
health=10;
sex='雄';
System.out.println("这不是构造方法");
}
public void print(){
System.out.println("企鹅的名字是:"+name+",健康度:"+health+",性别是:"+sex);
}
public static void main(String[] args) {
/*
* 这个Penguin()是默认的无参构造方法,默认无参构造方法的方法体内没有做任何操作
* 所以,通过默认的无参构造方法创造对象,对象的所有属性的属性值都是默认值
* 整数类型的默认值都是0,浮点类型的默认值都是0.0,char类型的默认值都是空格,
* 布尔类型的默认值都是false,引用类型的默认值都是null,
*
*/
Penguin pgn3=new Penguin();
pgn3.print();
}
}
方法重载
Java中存在两种多态:重载(overload)和重写(override),重写是与继承有关的多态;
所谓功能多态性,是指可以向功能传递不同的消息,以便让对象根据相应的消息来产生相应的行为。对象的行为通过类中的方法来体现,那么行为的多态性就是方法的重载。
方法重载:在同一个类中,出现多个方法的方法名相同,参数列表不同(参数的个数,参数类型,参数顺序;必须满足以上三个条件中的一个)的现象
1)在同一个类中
2)方法名相同
3)参数列表不同
4)与返回值类型,访问权限修饰符无关
public class Dog {
//定义属性
String name;
int age;
String gender;
String color;
String brand;
String weight;
//默认无参构造方法
public Dog(){
}
//有参构造方法
public Dog(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public Dog(String name, int age, String gender, String color, String brand,
String weight) {
this.name = name;
this.age = age;
this.gender = gender;
this.color = color;
this.brand = brand;
this.weight = weight;
}
//定义几个普通方法
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;
}
public double sum(double num1,int num2){
return num1+num2;
}
public int sum(int num1,int num2,int num3){
return num1+num2+num3;
}
public void test(){
System.out.println("test()");
}
public void demo(){
this.test();
}
}
this关键字
this是Java的一个关键字,表示某个对象。this可以出现在实例方法和构造方法中,但不可以出现在类方法中。
this关键字出现在类的构造方法中时,代表使用该构造方法所创建的对象。
声明方法时,方法类型前面不使用static修饰的是实例方法,用static修饰的是类方法,也称类方法。与实例无关
实例方法只能通过对象来调用,不能用类名来调用。当this关键字出现在实例方法中时,this就代表正在调用该方法的当前对象。
全局变量与局部变量
变量作业域:变量按照其所在的位置,可以分为成员变量(全局变量),局部变量两大类 变量作业域:变量按照其所在的位置,可以分为成员变量(全局变量),局部变量两大类
成员变量:
作用类中其它结构外的的变量,
成员变量的作用范围是整个类中都可以使用( 在静态方法中不能使用非静态的成员变量,可以使用静态的成员变量)
成员变量未赋值时,系统会给它复制一个默认值
在同一个类中,不能有同名的全局变量,全局变量和局部变量可以同名,使用时,局部变量比全局变量具有更高的优先级
局部变量:
作用方法中或者其它结构内的变量
局部变量的作用范围只限于定义局部变量的结构中
局部变量没有默认值,在使用之前要进行赋值,否则会报错
在不同的方法内(或者其它结构内)可以有相同名称的局部变量,在一个方法或者同一个结构内不能有同名的局部变量
成员变量和局部变量的区别
1)作用域不同
局部变量的作用域仅限于定义它的方法
成员变量的作用域在整个类内部都是可见的
2)初始值不同
Java会给成员变量(其未赋值时)一个初始值
Java不会给局部变量赋予初始值
public class Demo01 {
String name;
// String name;
int num1=1000;
public void test(){
int num1;
System.out.println(name);
num1=100;
//局部变量在使用之前一定要赋值
System.out.println(num1);
}
public void demo(){
System.out.println(num1);
int num1=100;
System.out.println(num1);
}
public static void ttt(){
//静态方法内不能使用非静态的成员变量
// System.out.println(name);
}
public static void main(String[] args) {
for (int i = 0; i <=10; i++) {
System.out.println(i);
}
//System.out.println(i);
//System.out.println(name);//非静态
//创建Demo01对象
Demo01 d=new Demo01();
d.test();
}
}
边栏推荐
猜你喜欢
2022年最新一篇文章教你青龙面板拉库,拉取单文件,安装依赖,设置环境变量,解决没有或丢失依赖can‘t find module之保姆教程(附带几十个青龙面板脚本仓库)
给你一个大厂面试的机会,你能面试上吗?进来看看!
Foundry教程:使用多种方式编写可升级的智能代理合约(下)
JDBC--Druid数据库连接池以及Template基本用法
centos安装mysql8
5.合宙Air32F103_LCD_key
合奥科技网络 面试(含参考答案)
JSP WebSehll 后门脚本
MySQL8 -- use msi (graphical user interface) under Windows installation method
(转帖)hashcode和equals的关系
随机推荐
嘉为蓝鲸携手东风集团、上汽零束再获信通院四项大奖
就瞎写=感想
Common SQL interview questions: 50 classic examples
HCIP Day 11_MPLS Experiment
Istio微服务治理网格的全方面可视化监控(微服务架构展示、资源监控、流量监控、链路监控)
OD-Model [4]: SSD
直击程序员面试现场:百度面试官都问了我些啥?
[LeetCode] 83. Delete duplicate elements in the sorted list
TRICK第二弹
Difference between #{} and ${}
7-42 整型关键字的散列映射 (25 分)
AntV X6制作画板工具(图形,线段,图片上传)
基于可逆网络的单一图像超分辨率
(转帖)hashcode和equals的关系
小程序(开发必备常识)1
rem适配
Foundry教程:使用多种方式编写可升级的智能代理合约(下)
JDBC的入门使用
Go语学习笔记 - gorm使用 - 事务操作 Web框架Gin(十一)
总体写作原则