我是靠谱客的博主 老实小熊猫,这篇文章主要介绍java 类和对象,循环,数组类:就是由关键字class生成的类对象:就是由类实例化出来的具体对象,现在分享给大家,希望可以做个参考。

类:就是由关键字class生成的类

对象:就是由类实例化出来的具体对象

student.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
package com.yangliwei; public class student { String name; int age; float shengao; int xuehao; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public float getShengao() { return shengao; } public void setShengao(float shengao) { this.shengao = shengao; } public int getXuehao() { return xuehao; } public void setXuehao(int xuehao) { this.xuehao = xuehao; } @Override public String toString() { return "student{" + "name='" + name + ''' + ", age=" + age + ", shengao=" + shengao + ", xuehao=" + xuehao + '}'; } public void out(String a){ System.out.println(a); } }

test.java //是一个main方法,里面实例化出来了一个学生类

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.yangliwei; public class test { public static void main(String[] args) { student stu1 = new student(); stu1.setName("杨利伟"); stu1.setAge(21); stu1.setXuehao(100001); stu1.setShengao(180.0F); System.out.println(stu1.toString()); stu1.out("yangliwei"); } }

循环

复制代码
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
public static void forxunhuan(){ int sum = 0 ; for (int i = 0;i<=100;i++){ sum += i; } System.out.println(sum); } public static void whilexunhuan(){ int sum = 0 ; int i = 0; while(i<=100){ sum +=i; i++; } System.out.println(sum); } public static void dowhile(){ int sum = 0 ; int i = 0; do{ sum+=i; i++; }while(i<=100); System.out.println(sum); }

数组

复制代码
1
2
3
4
5
6
public static void array(){ int[] a = {123,234,345,234,1234}; for (int i =0;i<=a.length-1;i++){ System.out.print(a[i]+","); }; }

 

最后

以上就是老实小熊猫最近收集整理的关于java 类和对象,循环,数组类:就是由关键字class生成的类对象:就是由类实例化出来的具体对象的全部内容,更多相关java内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(76)

评论列表共有 0 条评论

立即
投稿
返回
顶部