当前位置:网站首页>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
}
}
边栏推荐
- AMQP协议详解
- What do interview test engineers usually ask?The test supervisor tells you
- 测试技术之APP蓝牙连接测试
- Detailed explanation of the software testing process (mind map) of the first-tier manufacturers
- MySql copies data from one table to another table
- "Digital reconstruction of the system, getting the CEO is the first step"
- golang环境详细安装、配置
- 面试测试工程师一般会问什么?测试主管告诉你
- PSQL function, predicate, CASE expression and set operations
- MySQL导入sql文件的三种方法
猜你喜欢

Go语言之interface详解

MYSQL unique constraint

C language: Check for omissions and fill in vacancies (3)

Detailed explanation of the software testing process (mind map) of the first-tier manufacturers

ATM系统

MySQL导入sql文件的三种方法

JUC(一)- JUC学习概览 - 对JUC有一个整体的认识

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

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

apisix-入门使用篇
随机推荐
ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法
MySQL 8.0.29 设置和修改默认密码
nacos注册中心
AMQP协议详解
Google 安装印象笔记剪藏插件
51单片机外设篇:ADC
coredns介绍
Navicat报错:1045 -拒绝访问用户[email protected](使用passwordYES)
Google Chrome(谷歌浏览器)安装使用
MySQL 5.7升级到8.0详细过程
leetcode 665. Non-decreasing Array 非递减数列(中等)
leetcode 204. Count Primes 计数质数 (Easy)
Detailed installation and configuration of golang environment
navicat connects to MySQL and reports an error: 1045 - Access denied for user 'root'@'localhost' (using password YES)
go项目的打包部署
51单片机外设篇:红外通信
MySQL 多表关联一对多查询实现取最新一条数据
Redis集群模式
非关系型数据库MongoDB的特点及安装
LeetCode brush topic series - 787 K station transfer within the cheapest flight