首页 > 其他分享 >面向对象进阶static-静态变量

面向对象进阶static-静态变量

时间:2022-11-18 22:44:41浏览次数:68  
标签:return 进阶 gender age 面向对象 String static public name

面向对象进阶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

相关文章

  • 面向对象进阶:static的注意事项
    ......
  • 面向对象的五大基本原则
    单一职责原则单一职责原则的核心思想是:一个类最好只做一件事,只有一个引起它变化的原因。单一职责原则可以看作高内聚,低耦合在面向对象原则上的引申,将职责定义为引起变化的......
  • Java进阶篇——设计模式
    设计模式一、代理模式使用代理类对真实对象进行代理,包括真实对象方法的调用、功能的扩展等。访问的时候也只能访问到代理对象,既保护了真实对象同时可以在原始对象上进行......
  • Day8-4 static关键字详解
    static关键字详解packagecom.oop.demo07;​//staticpublicclassStudent{privatestaticintage;//静态变量privatedoublescore;//非静态变量......
  • 面向对象基础
    一、理解面向对象面向对象就是将编程当成是一个事物,对外界来说,事物是直接使用的,不用去管它内部的情况。而编程就是设置事物能够做什么事。二、类和对象在面向对......
  • 进阶道路
    C语言要想达到大厂标准,重点是指针和内存管理,以后可以去做服务器开发,后台开发,就包括驱动开发,进阶书籍《CPrimerPlus》、《C和指针》、《C专家编程》,但是由于C++比C多了面......
  • 幸运抽奖(面向对象)
    User:packagecom.javasm.bean;/***@Author:Zxb*@Version:1.0*@Date:2022/11/17-16:36*@Since:jdk1.8*@Description:*/publicclassUser{publici......
  • 2022.11.17 Java 面向对象的介绍
    属性+方法=类面向过程与面向对象不可分割,面向对象是由一个个面向过程组成,就像教育部由一个个老师组成。面向对象的本质:以类的方式组织代码,以对象的形式组织(封装)数据;......
  • 4.mysql-进阶
    1.事务将多个操作步骤变成一个事务,任何一个步骤失败,则回滚到事务的所有步骤之前状态,大白话:要成功都成功;要失败都失败。如转账操作,A扣钱。B收钱,必须两个步骤都成功,才认为......
  • C++ Tips:static const size_t nops、string substr、upper_bound、find()
    维基百科连接......