封装+构造方法小例子
生活随笔
收集整理的這篇文章主要介紹了
封装+构造方法小例子
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
public class FirstDemo {
??/**
????* 封裝+構(gòu)造方法小例子
????*/
??//
??private String student;
??private String name;
??private float math;
??private float english;
??private float computer;
??public String getStudent() {
????return student;
??}
??public void setStudent(String student) {
????this.student = student;
??}
??public String getName() {
????return name;
??}
??public void setName(String name) {
????this.name = name;
??}
??// 屬性
??public float getMath() {
????return math;
??}
??public void setMath(float math) {
????this.math = math;
??}
??public float getEnglish() {
????return english;
??}
??public void setEnglish(float english) {
????this.english = english;
??}
??public float getComputer() {
????return computer;
??}
??public void setComputer(float computer) {
????this.computer = computer;
??}
??// 方法
??public FirstDemo() {
????super();
????// 無參構(gòu)造
??}
??public FirstDemo(String s, String n, float m, float e, float c) {
????// 含參數(shù)構(gòu)造
????this.setStudent(s);
????this.setName(n);
????this.setMath(m);
????this.setEnglish(e);
????this.setComputer(c);
??}
??public float sum() {
????// 求和
????return math + english + computer;
??}
??public float avg() {
????// 平均數(shù)
????return this.sum() / 3;
??}
??public float max() {
????// 三科中的最大值
????float max = math;// 初始化數(shù)學(xué)為最高成績(jī)
????// 三目運(yùn)算符---如果數(shù)學(xué)成績(jī)大于計(jì)算機(jī)成績(jī),max=數(shù)學(xué)成績(jī)否則max=computer
????// 三目運(yùn)算符---如果數(shù)學(xué)成績(jī)大于英語(yǔ)成績(jī),max=數(shù)學(xué)成績(jī)否則max=english
????// 通過兩次運(yùn)算獲得三科中最大值
????max = max > computer ? max : computer;
????max = max > english ? max : english;
????return max;
??}
??public float min() {
????// 三科中的最小值
????float min = math;// 初始化數(shù)學(xué)為最高成績(jī)
????// 三目運(yùn)算符---如果數(shù)學(xué)成績(jī)大于計(jì)算機(jī)成績(jī),min=數(shù)學(xué)成績(jī)否則min=computer
????// 三目運(yùn)算符---如果數(shù)學(xué)成績(jī)大于英語(yǔ)成績(jī),min=數(shù)學(xué)成績(jī)否則min=english
????// 通過兩次運(yùn)算獲得三科中最大值
????min = min < computer ? min : computer;
????min = min < english ? min : english;
????return min;
??}
??public static void main(String[] args) {
????// 具體賦值
????FirstDemo firstDemo = new FirstDemo("01", "a1", 89, 98, 33);
????System.out.print("學(xué)生編號(hào):" + firstDemo.getStudent());
????System.out.print("\t學(xué)生名稱" + firstDemo.getName());
????System.out.print("\t數(shù)學(xué)成績(jī)" + firstDemo.getMath());
????System.out.print("\t英語(yǔ)成績(jī)" + firstDemo.getEnglish());
????System.out.print("\t計(jì)算機(jī)成績(jī)" + firstDemo.getComputer());
????System.out.print("\t總成績(jī)" + firstDemo.sum());
????System.out.print("\n平均分" + firstDemo.avg());
????System.out.print("\n最大值" + firstDemo.max());
????System.out.print("\n最小值" + firstDemo.min());
??}
}
??/**
????* 封裝+構(gòu)造方法小例子
????*/
??//
??private String student;
??private String name;
??private float math;
??private float english;
??private float computer;
??public String getStudent() {
????return student;
??}
??public void setStudent(String student) {
????this.student = student;
??}
??public String getName() {
????return name;
??}
??public void setName(String name) {
????this.name = name;
??}
??// 屬性
??public float getMath() {
????return math;
??}
??public void setMath(float math) {
????this.math = math;
??}
??public float getEnglish() {
????return english;
??}
??public void setEnglish(float english) {
????this.english = english;
??}
??public float getComputer() {
????return computer;
??}
??public void setComputer(float computer) {
????this.computer = computer;
??}
??// 方法
??public FirstDemo() {
????super();
????// 無參構(gòu)造
??}
??public FirstDemo(String s, String n, float m, float e, float c) {
????// 含參數(shù)構(gòu)造
????this.setStudent(s);
????this.setName(n);
????this.setMath(m);
????this.setEnglish(e);
????this.setComputer(c);
??}
??public float sum() {
????// 求和
????return math + english + computer;
??}
??public float avg() {
????// 平均數(shù)
????return this.sum() / 3;
??}
??public float max() {
????// 三科中的最大值
????float max = math;// 初始化數(shù)學(xué)為最高成績(jī)
????// 三目運(yùn)算符---如果數(shù)學(xué)成績(jī)大于計(jì)算機(jī)成績(jī),max=數(shù)學(xué)成績(jī)否則max=computer
????// 三目運(yùn)算符---如果數(shù)學(xué)成績(jī)大于英語(yǔ)成績(jī),max=數(shù)學(xué)成績(jī)否則max=english
????// 通過兩次運(yùn)算獲得三科中最大值
????max = max > computer ? max : computer;
????max = max > english ? max : english;
????return max;
??}
??public float min() {
????// 三科中的最小值
????float min = math;// 初始化數(shù)學(xué)為最高成績(jī)
????// 三目運(yùn)算符---如果數(shù)學(xué)成績(jī)大于計(jì)算機(jī)成績(jī),min=數(shù)學(xué)成績(jī)否則min=computer
????// 三目運(yùn)算符---如果數(shù)學(xué)成績(jī)大于英語(yǔ)成績(jī),min=數(shù)學(xué)成績(jī)否則min=english
????// 通過兩次運(yùn)算獲得三科中最大值
????min = min < computer ? min : computer;
????min = min < english ? min : english;
????return min;
??}
??public static void main(String[] args) {
????// 具體賦值
????FirstDemo firstDemo = new FirstDemo("01", "a1", 89, 98, 33);
????System.out.print("學(xué)生編號(hào):" + firstDemo.getStudent());
????System.out.print("\t學(xué)生名稱" + firstDemo.getName());
????System.out.print("\t數(shù)學(xué)成績(jī)" + firstDemo.getMath());
????System.out.print("\t英語(yǔ)成績(jī)" + firstDemo.getEnglish());
????System.out.print("\t計(jì)算機(jī)成績(jī)" + firstDemo.getComputer());
????System.out.print("\t總成績(jī)" + firstDemo.sum());
????System.out.print("\n平均分" + firstDemo.avg());
????System.out.print("\n最大值" + firstDemo.max());
????System.out.print("\n最小值" + firstDemo.min());
??}
}
轉(zhuǎn)載于:https://blog.51cto.com/haiyuanxi/913679
總結(jié)
以上是生活随笔為你收集整理的封装+构造方法小例子的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle数据误操作恢复【flashb
- 下一篇: 简单的家庭无线路由设置