当前位置:网站首页>Constructors, member variables, local variables
Constructors, member variables, local variables
2022-08-02 06:19:00 【Orion Li Tiezhu】
* 变量作用域:变量按照其所在的位置,可以分为成员变量(全局变量)、局部变量两大类
*
* 成员变量:
* 作用类中其它结构外的变量,
* 成员变量的作用范围是整个类中都可以使用(在静态方法中不能使用非静态的成员变量,可以使用静态的成员变量)
* 成员变量系统会给它赋值一个默认值
* 在同一个类中,不能有同名的全局变量,全局变量和局部变量可以同名,在使用的时候,局部变量具有更高的优先级
* 局部变量:
* 作用方法中或者其它结构内的变量,
* 局部变量的作用范围只限于定义局部变量的结构中
* 局部变量没有默认值,在使用之前要进行赋值,否则会报错
* 在不同的方法内(获取其它结构内)可以有相同名称的局部变量,在同一个方法或者结构内不能有同名的局部变量
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(){
//Static methods can't use a static member variables in
// 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;
//注意:This method is not a constructor,Only the method name not in accordance with the specification name,The method name is the name of the class
public void Penguin() {
health = 10;
sex = "雄";
System.out.println("执行构造方法");
}
public void print() {
System.out.println("企鹅的名字是" + name + ",健康值是" + health + ",性别是" + sex);
}
public static void main(String[] args) {
/*
* The first partyPenguin()Is the default constructor refs,The default no arguments constructor method didn't do any operation in the body
* 所以,By default arguments constructor to create objects,All attributes of the object attribute values are default values.
* The default value of integer types are0,The default value of floating point Numbers are0.0,boolean类型的默认值是false,charType the default value is blank,引用数据类型默认值是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();
//The above properties of the object is a an assignment,比较麻烦,能不能在创建对象的时候一次性赋值?--》通过有参构造函数来实现
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
}
}
边栏推荐
- Linux CentOS8安装Redis6
- 公司不重视软件测试,新来的阿里P8给我们撰写了测试用例编写规范
- H5 access payment process - WeChat payment & Alipay payment
- el-input 只能输入整数(包括正数、负数、0)或者只能输入整数(包括正数、负数、0)和小数
- 去字节跳动自动化测试二面原题(根据录音整理)真实有效 26
- 51单片机外设篇:红外通信
- 高防服务器防御的原理是什么
- Packaging and deployment of go projects
- [Digital IC hand-tear code] Verilog fixed priority arbiter | topic | principle | design | simulation
- eggjs controller层调用controller层解决方案
猜你喜欢
随机推荐
golang's time package: methods for time interval formatting and output of timestamp formats such as seconds, milliseconds, and nanoseconds
卸载redis
go项目的打包部署
Mysql 回表
165.比较版本号
Navicat报错:1045-Access denied for user [email protected](using passwordYES)
Grid布局介绍
LeetCode刷题系列 -- 787. K 站中转内最便宜的航班
MySQL 5.7详细下载安装配置教程
51单片机外设篇:红外通信
H5如何实现唤起APP
MySql copies data from one table to another table
golang generics
ApiPost is really fragrant and powerful, it's time to throw away Postman and Swagger
对node工程进行压力测试与性能分析
软件测试的需求人才越来越多,为什么大家还是不太愿意走软件测试的道路?
ATM系统
Redis-集群模式(主从复制模式,哨兵模式,集群化模式)
Brush LeetCode topic series - 10. Regular expression match
提高软件测试能力的方法有哪些?看完这篇文章让你提升一个档次