Java 基础类之二
Java 属性与局部变量的相同点与不同点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | package com.chenxi.java; /* 类中属性的使用 属性(成员变量)vs 局部变量 1.相同点 1.1定义变量的格式:数据类型 变量名= 变量值 1.2先声明后使用 1.3都有其对应作用域 2.不同点 2.1在类中声明的位置的不同 属性:直接定义在类的一对{}里 局部变量:声明在方法内、方法形参、代码块内、构造器形参构造器内部的 2.2关于权限修饰符的不同 属性:可以在声明属性时,指明其权限修饰符,使用权限修饰符; 常用的权限修饰符:private、public、缺省、protected; 局部变量不可以使用权限修饰符的 2.3 默认初始化值的情况 属性:类的属性,根据其类型,都有默认初始化值 整型:(byte、short、int、long):0 浮点型:(float、double):0.0 字符型:(char):0(或者'\u0000') 布尔型:(boolean):false 引用数据类型:(类、数组、接口)null 局部变量:没有默认初始化值 意味着,在调用之前一定要赋值 特别:形参在调用时我们赋值 2.4加载内存空间不同 属性:加载到堆空间中(非static) 局部变量:加载到栈中 */ public class usertest { public static void main(String[] arges){ Useri u1 = new Useri(); System.out.println(u1.yy); System.out.println(u1.age); u1.talk( "汉语" ); u1.eat(); } } class Useri{ //属性(成员变量) String yy; public int age; boolean imMale; public void talk(String language){ //language:形参,也是局部变量 System.out.println( "我们使用" +language+ "进行交流" ); } public void eat(){ String food= "烙饼" ; //局部变量 System.out.println( "北方人喜欢吃" +food); } } |
测试
1 2 3 4 | null 0 我们使用汉语进行交流 北方人喜欢吃烙饼 |
方法的使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | package com.chenxi.java; /* 类中方法的声明和使用 方法:描述类应该具有的功能 1.声明 public void ead(){}//void没有返回值的 public void sleep(int hour){}//形参 public String getName(){}//String 有返回值的 public String getNation(String nation){}//形参 2.方法的声明:权限修饰符 返回值类型 方法名(形参列表){ 方法体 } 注意 static、final、abstract 来修饰的方法 3.说明 3.1权限修饰符 Java规定的四种权限修饰符:private、public、缺省、protected 3.2返回值类型:有返回值vs没有返回值 3.2.1如果方法有返回值,则必须在方法声明时指定返回值类型。同时方法使用ruturn关键字来返回指定类型的变量或常量: “return数据”。 如果没有返回值,则声明时使用viod来表示。通常,没有返回值的方法里通常不使用return了。 如果使用的话,只能“return”来表示结束的意思 3.2.2定义方法时是否需要返回值 要求返回 凭经验 3.3方法名:属于标识符,遵循标识符的命名规则和规范,“见名知意” 形参列表:方法可以声明0个、1个、或者多个形参。 格式:数据类型1形参1,数据类型2形参2, 定义方法时,该不该定义形参。<br>1题目要求<br>2凭经验具体问题具体分析<br>方法体:方法功能的具体体现 return 关键字的 */ public class Customertest { public static void main(String[] age){ Customer cu1= new Customer(); cu1.ead(); } } class Customer{ String name; int age; boolean ismale; public void ead(){ //private void ead(){//私有别墅 System.out.println( "客户吃饭" ); } public void sleep( int hour){ System.out.println( "休息了" +hour+ "ge 小时" ); } public String getName(){ return name; } public String getNation(String nation){ String info = "我的国家是" + nation; return info; } } |
测试
1 | 客户吃饭 |
类方法创建规范
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | package com.chenxi.java; /* 类中方法的声明和使用 方法:描述类应该具有的功能 1.声明 public void ead(){}//void没有返回值的 public void sleep(int hour){}//形参 public String getName(){}//String 有返回值的 public String getNation(String nation){}//形参 2.方法的声明:权限修饰符 返回值类型 方法名(形参列表){ 方法体 } 注意 static、final、abstract 来修饰的方法 3.说明 3.1权限修饰符 Java规定的四种权限修饰符:private、public、缺省、protected 3.2返回值类型:有返回值vs没有返回值 3.2.1如果方法有返回值,则必须在方法声明时指定返回值类型。同时方法使用ruturn关键字来返回指定类型的变量或常量: “return数据”。 如果没有返回值,则声明时使用viod来表示。通常,没有返回值的方法里通常不使用return了。 如果使用的话,只能“return”来表示结束的意思 3.2.2定义方法时是否需要返回值 要求返回 凭经验 3.3方法名:属于标识符,遵循标识符的命名规则和规范,“见名知意” 3.4形参列表:方法可以声明0个、1个、或者多个形参。 3.4.1格式:数据类型1形参1,数据类型2形参2,... 3.4.2我们定义方法时,该不该定义形参 3.4.2.1题目要求 3.4.2.2凭经验,具体问题具体分析 3.5方法体:方法的具体实现 4.return 关键字 4.1使用范围:使用在方法体 4.2作用: 结束方法 针对返回值类型的方法,使用“return数据”方法返回所有的数据。 4.3注意点:return关键字后面不能声明执行语句 5.方法的使用中,可以调用当前类的属性或方法 特殊的:方法A中有调用了方法A:递归方法 方法中不可以定义方法 */ public class Customertest { public static void main(String[] age){ Customer cu1= new Customer(); cu1.ead(); cu1.sleep( 10 ); } } class Customer{ String name; int age; boolean ismale; public void ead(){ //private void ead(){//私有别墅 System.out.println( "客户吃饭" ); return ; } public void sleep( int hour){ System.out.println( "休息了" +hour+ "ge 小时" ); //ead(); //sleep(10);//栈溢出 } public String getName(){ return name; } public String getNation(String nation){ String info = "我的国家是" + nation; return info; } } |
测试题
类的声明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | package com.chenxi; public class bean { String name; int age; /** * sex : 1表示男性 * sex: 2表示女性 */ int sex; public void study(){ System.out.println( "studying" ); } public void showAge(){ System.out.println( "age" +age); } public int addAge( int i){ age += i; return age; } } //class Exer3test{ // public void method(){ // for (int i=0;i<10;i++){ // for (int j=0;j<8;j++){ // System.out.print("* "); // } // System.out.println(); // } // } //} //class Exer3test{ // public int method(){ // for (int i = 0;i<10;i++){ // for (int j =0; j<8;j++){ // System.out.print("* "); // } // System.out.println(); // } // return 10 * 8; // } //} class Exer3test{ public int method( int m, int n){ for ( int i= 0 ;i<m;i++){ for ( int j= 0 ;j<n;j++){ System.out.print( "* " ); } System.out.println(); } return m * n; } } |
类的调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | package com.chenxi; public class beantest { public static void main(String[] args){ //bean b1 = new bean(); //b1.name="tom"; //b1.age=18; //b1.sex=1; //b1.study(); //b1.showAge(); //int newage=b1.addAge(3);// //System.out.println(newage); //System.out.println(b1.age); Exer3test E1= new Exer3test(); //E1.method(2,10); int as = E1.method( 2 , 10 ); //System.out.println("面积为"+as+"平方"); System.out.println(as); } } 测试 * * * * * * * * * * * * * * * * * * * * 20 |
test
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | package com.chenxi; public class StudentTest { public static void main(String[] ages){ // Student s1 =new Student(); Student[] stus = new Student[ 20 ]; for ( int i = 0 ;i<stus.length;i++){ //给数组对象赋值 stus[i] = new Student(); //给student对象的属性赋值 stus[i].number=(i+ 1 ); //年级:[1-6] stus[i].state =( int )(Math.random() * ( 6 - 1 + 1 )+ 1 ); //成绩:[0,100] stus[i].score =( int )(Math.random()*( 100 - 0 + 1 )); } //打印三年级学生成绩 // for (int i =0 ; i<stus.length;i++){ // if (stus[i].state == 3){ // System.out.println(stus[i].number + ","+stus[i].state+"," + stus[i].number); // // } // } //使用冒泡排序 for ( int i = 0 ;i < stus.length- 1 ;i++ ){ for ( int j = 0 ; j< stus.length- 1 -i; j++){ //如果需要换序,交换的是数组的元素;Students对象! if (stus[j].score > stus[j+ 1 ].score){ Student temp = stus[j]; stus[j] = stus[j+ 1 ]; stus[j+ 1 ] = temp; } } } //遍历数组 for ( int i = 0 ; i < stus.length;i++){ System.out.println(stus[i].number + "," +stus[i].state+ "," + stus[i].score); } } } class Student{ int number; //学号 int state; //年龄 int score; //成绩 } |
测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 10 , 5 , 4 2 , 1 , 6 11 , 4 , 11 14 , 4 , 13 8 , 3 , 14 18 , 6 , 14 20 , 4 , 24 16 , 2 , 28 6 , 3 , 32 17 , 1 , 38 12 , 4 , 40 15 , 5 , 47 5 , 2 , 54 13 , 2 , 58 3 , 6 , 71 9 , 6 , 75 4 , 6 , 83 19 , 1 , 87 1 , 3 , 92 7 , 2 , 92 Process finished with exit code 0 |
草都可以从石头缝隙中长出来更可况你呢
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏