Day12-面向对象

Day12-面向对象

书接上回,继续对象学习

this关键字

this:代表“当前实例”,即模板中的当前对象,模板服务于哪个队象,this就指向哪个对象

第一种用法:调用本类中的实例属性、实例方法。可以省略,单当属性重名时不可省略

第二种方法:调用本类中的其他构造方法,调用时要放在构造方法的首行

public class Car {
	//属性
	String brand;
	String color;
	double price;
	
	//构造方法
	public Car() {
		
	}
	
	
	public Car(String brand, String color, double price) {
		this.brand = brand;
		this.color = color;
		this.price = price;
	}


	//方法
	public void run() {
		System.out.println(brand+"品牌,"+color+"颜色的汽车");
	}
	
	public void show() {
		System.out.println(brand+"\t"+this.color+"\t"+price);
	}
}

import java.util.Scanner;

public class TestCar {
	public static void main(String[] args) {
		
		Scanner input = new Scanner(System.in);
		
		Car c1 = new Car("大众","黑",100000);
		c1.run();
		c1.show();
		
		Car c2 = new Car("保时捷","白",5000000);
		c2.run();
		c2.show();
		
		System.out.println("==============================");
		//创建汽车数组,保存多个汽车,遍历汽车数组
		/*
		Car[] cars;
		cars = new Car[2];
		cars[0] = c1;
		cars[1] = c2;
		*/
		//键盘录入
		System.out.println("请输入仓库车位数量");
		int n = input.nextInt();
		Car[] cars= new Car[n];
		//循环录入
		for (int i = 0; i < cars.length; i++) {
			//两种方式
			//第一种方式
			Car car = new Car();
			System.out.println("请输入"+(i+1)+"号车库"+"汽车的品牌:");
			car.brand = input.next();
			//第二种方式
			//String brand = input.next();
			System.out.println("请输入"+(i+1)+"号车库"+"汽车的颜色:");
			car.color = input.next();
			//String color = input.next();
			System.out.println("请输入"+(i+1)+"号车库"+"汽车的价格:");
			car.price = input.nextDouble();
			//double price = input.nextDouble();
			
			//Car car = new Car(brand,color,price);
			//入库
			cars[i] = car;
		}
		
		//遍历
		for (Car car : cars) {
			car.run();
		}
	}
}

/**
 * 学生类
 */
public class Student {
	//特征:属性 实例变量
	String name;
	int age;
	String sex;
	double score;
	
	//构造方法
	public Student() {
		
	}
	public Student(String name,int age,String sex,double score) {
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.score = score;
	}
	
	//行为:方法 实例方法
	public void show() {
		System.out.println("大家好,我的名字是:"+name+",年龄是:"+age+",性别是:"+sex+",成绩是:"+score);
	}
	
	public void read() {
		this.show();
		System.out.println("阅读...");
	}
}

public class TestStudent {
	public static void main(String[] args) {
		
		//实例对象
		Student xm = new Student();
		//访问属性 存值
		//对象名.属性名 = 值;
		xm.name = "小明";
		xm.age = 18;
		xm.sex = "男";
		xm.score = 98.5;
		//访问方法
		//对象名.方法名();
		
		xm.read();
		
		
		//创建对象
		Student xh = new Student("小红",18,"女",99);
		
		xh.read();
	}
}

/**
 * 教师类
 */
public class Teacher {
	//特征:属性
	String name;
	int age;
	String sex;
	double salary;
	
	//构造方法
	public Teacher() {
		
	}
	
	
	public Teacher(String name, int age, String sex) {
		this.name = name;
		this.age = age;
		this.sex = sex;
		System.out.println("Teacher的三参构造");
	}
	
	public Teacher(String name, int age, String sex,double salary) {
		/*
		 * this.name = name; this.age = age; this.sex = sex; this.salary = salary;
		 */
		//调用三参方法
		this(name,age,sex);
		this.salary = salary;
		System.out.println("Teacher的四参构造");
	}


	//行为:方法
	public void show() {
		System.out.println("大家好,我的名字是:"+name+",年龄是:"+age+",性别是:"+sex+",薪资是:"+salary);
	}
}

public class TestTeacher {
	public static void main(String[] args) {
		Teacher tom = new Teacher("tom",28,"男");
		tom.show();
		
		Teacher jack = new Teacher("jack",30,"男",18000);
	}
}

新的篇章:三大特性(封装、继承、多态)

封装

概念:尽可能隐藏对象的内部实现细节,控制对象的修改及访问的权限

/**
 * 学生类
 */
public class Student {
	//属性
	private String name;
	private int age;
	private String sex;
	private double score;
	
	//构造方法
	public Student() {
		
	}
	//全参构造
	public Student(String name, int age, String sex, double score) {
		this.name = name;
		this.setAge(age);
		this.setSex(sex);;
		this.setScore(score);;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getName() {
		return name;
	}
	
	public void setAge(int age) {
		if (age > 0 && age < 140) {
			this.age = age;			
		}else {
			System.out.println("请输入正确的年龄");
		}
	}
	
	public int getAge() {
		return age;
	}
	
	public void setSex(String sex) {
		if ("男".equals(sex) || "女".equals(sex)) {
			this.sex = sex;			
		}else {
			System.out.println("请输入性别为男或女");
		}
	}
	
	public String getSex() {
		return sex;
	}
	
	public void setScore(double score) {
		if (score > 0.0 && score < 100.0) {
			this.score = score;			
		}else {
			System.out.println("请输入成绩在0~100分之间");
		}
	}
	
	public double getScore() {
		return score;
	}
	//方法
	public void show() {
		System.out.println("大家好,我的名字是:"+name+",年龄是:"+age+",性别是:"+sex+",成绩是:"+score);
	}
	
}

/**
 * 封装的步骤
 * 1、把属性变成私有的,在类外不能访问【属性前加private修饰】
 * 2、添加get(获取)和set(设置)方法
 */
public class TestStudent {
	public static void main(String[] args) {
		
		//创建对象
		Student xm = new Student();
		//访问属性
		xm.setName("小明");
		xm.setAge(16);
		xm.setSex("男");
		xm.setScore(98.5);
		
		//访问方法
		xm.show();
		
		Student xh = new Student("小红",5555,"hhh",199.0);
		xh.show();
	}
}

继承

类与类之间特征和行为的一种赠与或获得

两个类之间的继承关系,必须满足is a的关系

public class TestExtends {
	public static void main(String[] args) {
		Dog dog1 = new Dog();
		dog1.breed = "哈士奇";
		dog1.age = 2;
		dog1.sex = "公";
		dog1.furColor = "灰白色";
		
		Bird bird1 = new Bird();
		bird1.breed = "麻雀";
		bird1.age = 1;
		bird1.sex = "雄";
		bird1.furColor = "灰色";
	}
}

//父类
class Animal{
	String breed;
	String sex;
	int age;
	
	public void eat() {
		
	}
	public void sleep() {
		
	}
}

//子类
class Dog extends Animal{
	String furColor;
	public void run() {
		
	}
}
//子类
class Fish extends Animal{
	public void swim() {
		
	}
}
//子类
class Bird extends Animal{
	String furColor;
	public void fly() {
		
	}
}
//子类
class Snake extends Animal{
	public void climb() {
		
	}
}

public class TestExtends2 {
	public static void main(String[] args) {
		Car car = new Car();
		car.brand = "宝马";
		car.price = 300000;
		car.speed = 120;
		System.out.println("这是一辆"+car.brand+"品牌的汽车,正以"+car.speed+"km/s的速度行驶");
		
		Bus bus = new Bus();
		bus.seatNum = 8;
		bus.price = 1000000;
		bus.speed = 80;
		System.out.println("这是一辆"+bus.seatNum+"座的公交车,正以"+bus.speed+"km/s的速度行驶");
		
	}
}
//父类 交通工具类
class Vehicle{
	double price;
	int speed;
	public void run() {
		System.out.println("行驶中...");
	}
}
//子类
class Car extends Vehicle{
	String brand;//品牌
}
//子类
class Bus extends Vehicle{
	int seatNum;//座位数
}
//子类
class Bicycle extends Vehicle{
	String color;
}

访问修饰符

public class Test {
	public void show() {
		TestSelfClass t = new TestSelfClass();
		//同包下,除private都可见
		//System.out.println(t.a);
		System.out.println(t.b);
		System.out.println(t.c);
		System.out.println(t.d);
	}
}

/**
 * private
 * 访问本类
 * 四个访问权限符均有效
 */
public class TestSelfClass {
	private String a = "A";//私有的 红色正方形
	String b = "B";//default 默认的 蓝色的三角形
	protected String c = "C";//受保护的 黄色的菱形
	public String d = "D";//公共的 绿色的圆形
	
	public void show() {
		System.out.println(this.a);
		System.out.println(this.b);
		System.out.println(this.c);
		System.out.println(this.d);
	}
}

待完善...

posted @ 2021-07-23 21:35  CN_Darren  阅读(41)  评论(0)    收藏  举报