当前位置:网站首页>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
}
}
边栏推荐
- MySQL implements sorting according to custom (specified order)
- 一线大厂软件测试流程(思维导图)详解
- [PSQL] window function, GROUPING operator
- APP Bluetooth connection test of test technology
- 区块元素、内联元素(<div>元素、span元素)
- How much does a test environment cost? Start with cost and efficiency
- H5 access payment process - WeChat payment & Alipay payment
- 【解决】RESP.app 连接不上redis
- MySQL 5.7 detailed download, installation and configuration tutorial
- Timing task library in the language use Cron, rounding
猜你喜欢

面试测试工程师一般会问什么?测试主管告诉你

区块元素、内联元素(<div>元素、span元素)

Introduction to Grid Layout

18 years of programmer career, read more than 200 programming books, pick out some essence to share with you

51单片机外设篇:点阵式LCD

Google Chrome(谷歌浏览器)安装使用

apifox介绍及使用(1)。

mysql 8.0.28版本安装配置方法图文教程

The Go language learning notes - dealing with timeout - use the language from scratch from Context

公司不重视软件测试,新来的阿里P8给我们撰写了测试用例编写规范
随机推荐
How much does a test environment cost? Start with cost and efficiency
JDBC revisited
复盘:图像饱和度计算公式和图像信噪(PSNR)比计算公式
软件测试的需求人才越来越多,为什么大家还是不太愿意走软件测试的道路?
Introduction and use of apifox (1).
自动化运维工具——ansible、概述、安装、模块介绍
Navicat报错:1045 -拒绝访问用户[email protected](使用passwordYES)
Go语言中定时任务库Cron使用详解
MySQL 字符串拼接 - 多种字符串拼接实战案例
kubernetes affinity, anti-affinity, taint, tolerance
Redis集群模式
51单片机外设篇:ADC
Detailed installation and configuration of golang environment
MySQL 8.0.28 version installation and configuration method graphic tutorial
测试技术之APP蓝牙连接测试
C语言中i++和++i在循环中的差异性
C language: Check for omissions and fill in vacancies (3)
Detailed explanation of the software testing process (mind map) of the first-tier manufacturers
PSQL function, predicate, CASE expression and set operations
51单片机外设篇:红外通信