面向对象进阶static-静态变量
package Static.a01StaticDemo;
public class Student {
private String name;
private int age;
private char gender;
public static String TeacherName;
public Student() {
}
public Student(String name, int age, char gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
/**
* 获取
* @return gender
*/
public char getGender() {
return gender;
}
/**
* 设置
* @param gender
*/
public void setGender(char gender) {
this.gender = gender;
}
public String toString() {
return "Student{name = " + name + ", age = " + age + ", gender = " + gender + "}";
}
//行为
public void study(){
System.out.println(name+"正在学习");
}
public void show(){
System.out.println(name+", "+age+", "+gender+","+TeacherName);
}
}
package Static.a01StaticDemo;
public class Student {
private String name;
private int age;
private char gender;
public static String TeacherName;
public Student() {
}
public Student(String name, int age, char gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
/**
* 获取
* @return gender
*/
public char getGender() {
return gender;
}
/**
* 设置
* @param gender
*/
public void setGender(char gender) {
this.gender = gender;
}
public String toString() {
return "Student{name = " + name + ", age = " + age + ", gender = " + gender + "}";
}
//行为
public void study(){
System.out.println(name+"正在学习");
}
public void show(){
System.out.println(name+", "+age+", "+gender+","+TeacherName);
}
}
、
package Static.a02StaticDemo;
public class ArrayUtil {
//私有化构造方法
private ArrayUtil(){
}
//遍历数组
public static String printArr(int[]arr){
StringBuilder sb=new StringBuilder();
sb.append("[");
for (int i = 0; i < arr.length; i++) {
if (i== arr.length-1){
sb.append(arr[i]);
}else {
sb.append(arr[i]).append(", ");
}
}
sb.append("]");
return sb.toString();
}
//求平均值
public static double getAverage(double[]arr){
double sum=0;
for (int i = 0; i < arr.length; i++) {
sum=sum+arr[i];
}
return sum/arr.length;
}
}
package Static.a02StaticDemo;
public class Test {
public static void main(String[] args) {
//测试工具类中的两个方法是否正确
int[]arr={1,2,3,4,5,6};
String s = ArrayUtil.printArr(arr);
System.out.println(s);
double[]arr1={1.1,2.2,3.3,4.4,5.5};
double average = ArrayUtil.getAverage(arr1);
System.out.println(average);
}
}
package Static.a03StaticDemo;
public class ArrayUtil {
private String name;
private int age;
private char gender;
public ArrayUtil() {
}
public ArrayUtil(String name, int age, char gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
/**
* 获取
* @return gender
*/
public char getGender() {
return gender;
}
/**
* 设置
* @param gender
*/
public void setGender(char gender) {
this.gender = gender;
}
public String toString() {
return "ArrayUtil{name = " + name + ", age = " + age + ", gender = " + gender + "}";
}
}
package Static.a03StaticDemo;
import java.util.ArrayList;
public class ArrayUtil1 {
private ArrayUtil1(){
}
public static int getMaxAge(ArrayList<ArrayUtil> list){
int max = list.get(0).getAge();
for (int i = 1; i < list.size(); i++) {
int tempAge = list.get(i).getAge();
if (tempAge>max){
max=tempAge;
}
}
return max;
}
}
package Static.a03StaticDemo;
import StudentManagementSystem.Student;
import java.util.ArrayDeque;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
//创建一个集合用来存储数据对象
ArrayList<ArrayUtil>list=new ArrayList<>();
ArrayUtil au1=new ArrayUtil("zhangs",18,'男');
ArrayUtil au2=new ArrayUtil("lishi",28,'男');
ArrayUtil au3=new ArrayUtil("wangwu",38,'男');
list.add(au1);
list.add(au2);
list.add(au3);
int age = ArrayUtil1.getMaxAge(list);
System.out.println(age);
}
}
标签:return,进阶,gender,age,面向对象,String,static,public,name
From: https://www.cnblogs.com/Alluw/p/16905089.html