东秦抢红包02
2021-04-15 本文已影响0人
__method__
方法
package com.neusoft.two;
public class Demo1 {
public static void main(String[] args) {
// 实例化对象
// 类名 对象名 = new 类目()
Demo1 demo1 = new Demo1();
// 方法调用
demo1.caculate(50);
int res = demo1.caculateR(100);
System.out.println(res);
}
// 方法
// public 返回值 方法名(类型 形参1, 类型 形参2){
//
// }
public void caculate(){
// 1~100
int sum = 0;
for (int i = 1; i <= 100 ; i++) {
sum +=i;
}
System.out.println(sum);
}
public void caculate(int num){
// 1~num
int sum = 0;
for (int i = 1; i <= num ; i++) {
sum +=i;
}
System.out.println(sum);
}
public int caculateR(int num){
// 1~num
int sum = 0;
for (int i = 1; i <= num ; i++) {
sum +=i;
}
return sum;
}
}
ArrayList
package com.neusoft.two;
import java.util.ArrayList;
import java.util.Random;
public class Demo2 {
public static void main(String[] args) {
// 类名 对象名 = new 类目()
Demo2 demo2 = new Demo2();
// 方法调用
demo2.caculate(50);
// 动态数组 <> 是泛型的意思 对对象内数据类型的限制
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i <10 ; i++) {
Random random = new Random();
int num = 100 + random.nextInt(101);
System.out.print(num + "\t");
list.add(num);
}
System.out.println();
System.out.println(list);
System.out.println("单个访问");
for (int i = 0; i < list.size() ; i++) {
list.set(i, list.get(i) +100);
}
System.out.println("加100之后");
System.out.println(list);
}
public int caculate(int num){
// 1~100
int sum = 0;
for (int i = 1; i <= num ; i++) {
sum +=i;
}
return sum;
}
}
自定义对象的创建
package com.neusoft.two;
import java.util.ArrayList;
// JavaBean
public class Student {
// 属性
private String name;
// boolean sex;
private String sex;
ArrayList<Double> scoreList;
// 构造函数
public Student(){
}
public Student(String name, String sex) {
this.name = name;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public ArrayList<Double> getScoreList() {
return scoreList;
}
public void setScoreList(ArrayList<Double> scoreList) {
this.scoreList = scoreList;
}
// 方法
public void show(){
System.out.println("姓名" + name+ "性别"+ sex);
System.out.println("分数列表" + scoreList);
}
}
测试
package com.neusoft.two;
public class Test {
public static void main(String[] args) {
Student s1 = new Student("zhanan", "男");
System.out.print( s1.getName());
s1.setName("李四");
// s1.scoreList = 100;
s1.show();
}
}