当前位置:网站首页>构造方法、成员变量、局部变量
构造方法、成员变量、局部变量
2022-08-02 05:06:00 【猎户李铁柱】
* 变量作用域:变量按照其所在的位置,可以分为成员变量(全局变量)、局部变量两大类
*
* 成员变量:
* 作用类中其它结构外的变量,
* 成员变量的作用范围是整个类中都可以使用(在静态方法中不能使用非静态的成员变量,可以使用静态的成员变量)
* 成员变量系统会给它赋值一个默认值
* 在同一个类中,不能有同名的全局变量,全局变量和局部变量可以同名,在使用的时候,局部变量具有更高的优先级
* 局部变量:
* 作用方法中或者其它结构内的变量,
* 局部变量的作用范围只限于定义局部变量的结构中
* 局部变量没有默认值,在使用之前要进行赋值,否则会报错
* 在不同的方法内(获取其它结构内)可以有相同名称的局部变量,在同一个方法或者结构内不能有同名的局部变量
package cn.bdqn.demo06;
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 = 1; i <= 10; i++) {
System.out.println(i);
}
// System.out.println(i);
// System.out.println(name);
//创建Demo01对象
Demo01 d = new Demo01();
d.test();
}
}
* 方法重载:在同一个类中,出现多个方法的方法名相同,参数列表不同(参数的个数、参数的类型、参数顺序)的现象。
* 1)在同一个类中
* 2)方法名相同
* 3)参数列表不同
* 4)与返回值类型、访问权限修饰符无关
package cn.bdqn.demo04;
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();
}
}
package cn.bdqn.demo05;
public class Penguin {
String name;
int health;
String 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,boolean类型的默认值是false,char类型默认值是空格,引用数据类型默认值是null
*/
Penguin pgn3= new Penguin();
pgn3.print();
}
}
package cn.bdqn.demo01;
public class Student {
//定义属性
String name;
int age;
char gender;
//定义方法
public void printInfo(){
System.out.println("姓名:"+name);
System.out.println("年龄:"+age);
System.out.println("性别:"+gender);
}
}
package cn.bdqn.demo03;
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();
}
}
package cn.bdqn.demo03;
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;
}
}
package cn.bdqn.demo03;
public class TeacherTest {
public static void main(String[] args) {
Teacher teacher = new Teacher("王五", 22, '男', 189, 165, "安徽淮北", 89.5, 13888889999L);
}
}
package cn.bdqn.demo07;
public class Test {
public int num;
public void calc(int num, int num2) {
System.out.println(num + num2);
}
public static void main(String[] args) {
Test test = new Test();
test.num = 10;
test.calc(11, 3);//14
System.out.println(test.num);//10
}
}
边栏推荐
猜你喜欢

Packaging and deployment of go projects
[email protected](使用passwordYES)"/>Navicat报错:1045 -拒绝访问用户[email protected](使用passwordYES)

JUC(二)原子类:CAS、乐观锁、Unsafe和原子类

对node工程进行压力测试与性能分析

matlab simulink 飞机飞行状态控制

Navicat new database

Redis-集群模式(主从复制模式,哨兵模式,集群化模式)

MYSQL unique constraint

ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法

18 years of programmer career, read more than 200 programming books, pick out some essence to share with you
随机推荐
力扣 2127. 参加会议的最多员工数 拓扑剪枝与2360补充
说好的女程序员做测试有优势?面试十几家,被面试官虐哭~~
navicat connects to MySQL and reports an error: 1045 - Access denied for user 'root'@'localhost' (using password YES)
JUC(一)- JUC学习概览 - 对JUC有一个整体的认识
大屏UI设计-看这一篇就够了
Introduction to Grid Layout
高防服务器防御的原理是什么
Grid布局介绍
ApiPost is really fragrant and powerful, it's time to throw away Postman and Swagger
Mycat2.0搭建教程
coredns介绍
prisma使用mongodb副本集群报错引发的一些列问题
What do interview test engineers usually ask?The test supervisor tells you
go语言中的goroutine(协程)
The original question on the two sides of the automatic test of the byte beating (arranged according to the recording) is real and effective 26
165.比较版本号
mysql 存储过程详解
MySQL 5.7 upgrade to 8.0 detailed process
MySQL multi-table association one-to-many query to get the latest data
腾讯注册中心演进及性能优化实践