2019秋第三周学习总结

本周呢主要是学习了构造方法,以及Java面向对象的特性封装性
例:
题目:定义并测试一个名为Student的类,包括属性有“学号”,“姓名”、以及三门课程“数学”、“英语”和“计算机”的成绩,包括的方法有三门课程的“总分”、“平均分”、“最高分”及“最低分”。
实验代码

package text1;

class Student implements PersonUser {
	
	private String stuno;
	private String name;
	private float math;
	private float english;
	private float computer;
	
	public Student(String stuno, String name, float math, float english, float computer) {
		this.stuno = stuno;
		this.name = name;
		this.math = math;
		this.english = english;
		this.computer = computer;
	}

	@Override
	public String getName() {
		return name;
	}
	@Override
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String getStuno() {
		return stuno;
	}
	@Override
	public void setStuno(String stuno) {
		this.stuno = stuno;
	}
	@Override
	public float getMath() {
		return math;
	}
	@Override
	public void setMath(float math) {
		this.math = math;
	}
	@Override
	public float getEnglish() {
		return english;
	}
	@Override
	public void setEnglish(float english) {
		this.english = english;
	}
	@Override
	public float getComputer() {
		return computer;
	}
	@Override
	public void setComputer(float computer) {
		this.computer = computer;
	}
	
	@Override
	public float sum() {
		return math+english+computer;
	}
	
	@Override
	public float avg() {
		return this.sum()/3;
	}
	
	@Override
	public float max() {
		float max=math;
		max=max>computer?max:computer;
		max=max>english?max:english;
		return max;
	}
	
	@Override
	public float min() {
		float min=math;
		min=min<computer?min:computer;
		min=min<english?min:english;
		return min;
	}
}
package text1;

public class Ceishi {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Student per1=null;
		per1=new Student("1","张三",95.0f,89.0f,96.0f);
		System.out.println("学生编号:"+per1.getStuno());
		System.out.println("学生姓名:"+per1.getName());
		System.out.println("数学成绩:"+per1.getMath());
		System.out.println("英语成绩:"+per1.getEnglish());
		System.out.println("计算机成绩:"+per1.getComputer());
		System.out.println("总分:"+per1.sum());
		System.out.println("平均分:"+per1.avg());
		System.out.println("最高分:"+per1.max());
		System.out.println("最低分:"+per1.min());


	}

}

测试结果

还学习了this类、static类

this

属性访问:访问本类中的属性,如果本类没有此属性则从父类中继续查找。
方法:访问本类中的方法,如果本类没有此方法则从父类中继续查找。
调用构造:调用本类构造,必须放在构造方法的首行。
特殊:表示当前对象。

static

如果在程序中使用static声明属性的话,则此属性属于全局属性;static声明的属性是所有对象共享的,在访问static属性时最好可以由类名称直接调用。
static既可以在声明属性的时候使用,也可以用其来声明方法,用它声明的方法有时也被称为类方法,可以由类名称直接调用。
非static声明的方法可以去调用static声明的属性或方法的。但是static声明的方法是不能调用非static类型声明的属性或方法的

对于main()方法也进行了一些学习,知道了main()方法每个参数的含义。学会另一种循环方法去遍历数组,如下:

for(String e:args){
     System.out.println(e);
}
原文地址:https://www.cnblogs.com/H-Alice/p/11516374.html