Java笔记10-面向对象的编程-下(********) - Go语言中文社区

Java笔记10-面向对象的编程-下(********)



三条主线:
1.类和类的成员:属性,方法,构造器,代码块,内部类
2.封装,继承,多态
3.其它关键字的使用(this,package,import,super,static,final,abstract,接口-Interface)






public class StaticTest {
	
	public static void main(String[] args) {
		
		Chinese.nation = "Chinese";
		
		Chinese c = new Chinese();
		c.name = "姚明";
		c.age = 40;
		
		Chinese c1 = new Chinese();
		c1.name = "马龙";
		c1.age = 30;
		
		c.nation = "CHN";
		
		System.out.println(c1.nation);
		
		c1.eat();
		
		Chinese.show();
	}
}

class Chinese {
	
	String name;
	int age;
	static String nation;
	
	public void eat() {
		System.out.println("中国人吃馒头");
	}
	
	public static void show() {
		System.out.println("我是一个中国人!");
	}
	
}


static的应用举例:

public class CircleTest {

	public static void main(String[] args) {

		Circle c1 = new Circle();
		Circle c2 = new Circle();
		Circle c3 = new Circle(3.4);

		System.out.println("c1的id:" + c1.getId());
		System.out.println("c2的id:" + c2.getId());
		System.out.println("c3的id:" + c3.getId());
		
		System.out.println("创建圆的个数为:" + Circle.getTotal());
	}
}

class Circle {

	private double radius;
	private int id;  //自动赋值

	private static int total; // 记录创建圆的个数
	private static int init = 1001; // static生命的属性被所有对象所共享

	public Circle() {
		id = init++;
		total++;
	}
	
	public Circle(double radius) {
		//有时构造对象是用的含参的构造器,记录的个数同样要加加
		this();  //类似于调用了空参的构造器
//		id = init++;
//		total++;
		this.radius = radius;
	}

	public double getRadius() {
		return radius;
	}

	public void setRadius(double radius) {
		this.radius = radius;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public static int getTotal() {
		return total;
	}

	public double findArea() {
		return 3.14 * radius * radius;
	}
}





重写toString;

测试类:

应用:

经典的23中设计模式:

饿汉式-单例设计模式:


返回的是同一个对象;




单例设计模式的应用场景


通过main函数进行与控制台进行交互:

先正常编译运行一下;
接下来就是Run Configurations:


用命令行进行运行编译:


三条主线:
1.类和类的成员,属性,方法,构造器,代码块,内部类
2.封装,继承,多态
3.其它关键字的使用(this,package,import,super,static,final,abstract,接口-Interface)




小例子测试:
LeafTest.java:





Son.java:


此时的结果:

继续new一个Son的结果:

注释全部打开的结果:







每日一练:






public class AbstractTest {
	
	public static void main(String[] args) {
		
		//一旦Person类抽象了,就不可以实例化了
//		Person p1 = new Person();
//		p1.eat();
	}
}

abstract class Person {
	
	String name;
	int age;
	
	public Person() {
		
	}
	public Person(String name, int age) {
		this.name = name;
		this.age = age;
	}
	
	//这不是抽象方法(没有方法体的方法)
//	public void eat() {
//		
//	}
	
	//这是抽象方法
	public abstract void eats();
	
	public void eat() {
		System.out.println("人吃饭!");
	}
	
	public void walk() {
		System.out.println("人走路!");
	}
}

abstract class Student extends Person {
	
	public Student(String name, int age) {
		//构造器的另外作用:依然可以被子类调用含参构造器
		super(name,age);
	}
	
	//1.可以重写父类的所有方法来处理抽象方法
//	public void eats() {
//		System.out.println("学生们应该多吃营养食物!");
//	}
	//2.需要将这个子类置为抽象类
}




问题三答案:可以!





匿名类:



例:抽象类的应用:模板方法的设计模式
测试类:



应用二:





alt+shift+s:打开toString,get,set方法;

import java.util.Calendar;
import java.util.Scanner;

public class PayrollSystem {
	
	public static void main(String[] args) {
		//方式一
//		Scanner scanner = new Scanner(System.in);
//		System.out.println("请输入当月的月份:");
//		int month = scanner.nextInt();
		//方式二:
		Calendar calendar = Calendar.getInstance();
		int month = calendar.get(Calendar.MONTH);  //获取当前的月份
		System.out.println(month);  //一月为:0
		/*
		 *注:栈中有个变量是emps,指向堆空间造了一个数组,长度是2,
		 *每个位置声明成Employee类型 (数组元素暂时为抽象成类)
		 */
		Employee[] emps = new Employee[2];
		//这才是多态
		emps[0] = new SalariedEmployee("小明", 1002, new MyDate(1992, 2, 28),10000);
		emps[1] = new HourlyEmployee("潘雨声", 2001, new MyDate(1991, 3, 6), 60, 240);
		
		for(int i = 0;i < emps.length;i++) {
			System.out.println(emps[i]);
			double salary = emps[i].earnings();
			System.out.println("月工资为:" + salary);
			
			if( (month+1) == emps[i].getBirthday().getMonth()) {
				System.out.println("生日快乐!奖励100元");
			}
		}
	}
}
public abstract class Employee {

	private String name;
	private int number;
	private MyDate birthday;
	
	public Employee(String name, int number, MyDate birthday) {
		super();
		this.name = name;
		this.number = number;
		this.birthday = birthday;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

	public MyDate getBirthday() {
		return birthday;
	}

	public void setBirthday(MyDate birthday) {
		this.birthday = birthday;
	}

	public abstract double earnings();

	@Override
	public String toString() {
		return "name=" + name + ", number=" + number + ", birthday=" + birthday.toDateString();
	}
}
public class MyDate {

	private int year;
	private int month;
	private int day;

	public MyDate(int year, int month, int day) {
		super();
		this.year = year;
		this.month = month;
		this.day = day;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public int getDay() {
		return day;
	}

	public void setDay(int day) {
		this.day = day;
	}

	public String toDateString() {
		return year + "年" + month + "月" + day + "日";
	}
}
public class SalariedEmployee extends Employee {
	
	private double monthlySalary;//月工资
	
	//Employee没有空参的构造器,需要提供含有super(Employee)的构造器
	public SalariedEmployee(String name, int number, MyDate birthday) {
		super(name, number, birthday);
	}
	public SalariedEmployee(String name, int number, MyDate birthday, double monthlySalary) {
		super(name, number, birthday);
		this.monthlySalary = monthlySalary;
	}
	
	public double getMonthlySalary() {
		return monthlySalary;
	}

	public void setMonthlySalary(double monthlySalary) {
		this.monthlySalary = monthlySalary;
	}

	//有抽象方法,需要重写抽象方法--这样才能实例化
	@Override
	public double earnings() {
		return monthlySalary;
	}
	@Override
	public String toString() {
		return "SalariedEmployee [" + super.toString() + "]";
	}	
}	
public class HourlyEmployee extends Employee{
	
	private int wage;  //每小时的工资
	private int hour;  //月工作的小时数
	public HourlyEmployee(String name, int number, MyDate birthday) {
		super(name, number, birthday);
	}
	public HourlyEmployee(String name, int number, MyDate birthday, int wage, int hour) {
		super(name, number, birthday);
		this.wage = wage;
		this.hour = hour;
	}
	
	public int getWage() {
		return wage;
	}
	public void setWage(int wage) {
		this.wage = wage;
	}
	public int getHour() {
		return hour;
	}
	public void setHour(int hour) {
		this.hour = hour;
	}
	
	@Override
	public String toString() {
		return "HourlyEmployee [" + super.toString() + "]";
	}
	@Override
	public double earnings() {
		return wage * hour;
	}	
}


类和接口是两个并列的结构,类中是单继承,一个类可以实现多个接口。可以解决类的单继承性
版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/pfl_327/article/details/114034854
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

  • 发表于 2021-04-10 16:45:54
  • 阅读 ( 857 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢