第六章总结

对象:代表很抽象性的一种事物,对象又有动态属性(行为),静态属性(属性)两种

类(是同一类事物的统称):是对象的设计图,是封装对象属性和行为的载体

封装:把静态属性和动态属性放进类这个行为

封装的好处:提高代码的复用性(可以用很多遍)

继承:分父类子类,子类的个性都不一样,但是特性都是一样的,继承最下面一层的一定是子类,最上面的一定是父类,中间可以是父类也可以是子类

多态:多种形态,是一种特征

课堂内容:

public class Liua {//新建类
	//静态属性——成员变量
	private String name;//名字
	private String wing;//翅膀
	private String head;//头
	//getter 和 setter 方法
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getWing() {
		return wing;
	}
	public void setWing(String wing) {
		this.wing = wing;
	}
	public String getHead() {
		return head;
	}
	public void setHead(String head) {
		this.head = head;
	}
	//构造方法 调用后会产生一个对象
	public Liua() {
	}
	public Liua(String name) {
		this.name = name;
	}
	public Liua(String name, String wing, String head) {
		this.name = name;
		this.wing = wing;
		this.head = head;
	}
	
	//动态属性——成员方法
		String eat(String food) {
			return name + "在吃" + food;
		}
	String fly() {
	return name + "在飞";
}
}
public class Liub {

	public static void main(String[] args) {
		//neW 调用构造方法 产生一个对象
		Liua liua1 = new Liua();//成员变量
		liua1.setName("布谷鸟");//构造方法
		String fly = liua1.fly();//成员方法
		System.out.println(fly);
		
		Liua liua2 = new Liua("大雁");
		String eat = liua2.eat("谷物");
		System.out.print(eat);
	}
}
public class Liuc {
	//静态变量
	static double PI = 3.1415926;
	//静态常量
	static final int i = 3;
	//静态方法
	static void method() {//成员方法
		System.out.println("静态方法");
	}
	//静态代码块
	static
	{
		//1
		//2
		//3
		//4
		//5...
	}
	public static void main(String[] args) {
		System.out.println(Liuc.PI);
		System.out.println(Liuc.i);
		Liuc.method();
	}
}

例6.1代码

public class Liulta {//类
	private String name;//String类型的成员变量
	public String getName() {//name的Getter方法
		return name;
	}
	public void setName(String name) {//name的Setter方法
		this.name = name;//将参数值赋予类中的成员变量
	}
}

例6.2代码

public class Liultb {//类
public static int[] exLiultb(int[] arr) {
	int tmp = arr[0];//创建局部变量tmp,保存数组第一个元素得知
	arr[0] = arr[1];//第二个元素值赋给第一个元素
	arr[1] = tmp;//第二个元素值改为tmp
	return arr;
}

	public static void main(String[] args) {
		int arr[] = {17,29};
		System.out.println("第一个值=" + arr[0] + ",第二个值=" + arr[1]);
		arr = exLiultb(arr);
		System.out.println("第一个值=" + arr[0] + ",第二个值=" + arr[1]);
	}
}

输出结果

例6.3代码

public class Liultc {//类
	int eggCount;//鸡蛋灌饼里蛋的个数
	
	public Liultc(int eggCount) {//参数为鸡蛋灌饼里蛋的个数的构造方法
		this.eggCount = eggCount;//将参数eggCount的值付给属性eggCount
	}
	
	public Liultc() {//无参数构造方法,默认给饼加一个蛋
		//调用参数为鸡蛋灌饼里蛋的个数的构造方法,并设置鸡蛋灌饼里蛋的个数为1
		this(1);
	}
	
	public static void main(String[] args) {
		Liultc cake1 = new Liultc();
		System.out.println("顾客不要求加蛋的数量,饼里会有" + cake1.eggCount + "个蛋。");
		Liultc cake2 = new Liultc(2);
		System.out.println("顾客要求加2个蛋的数量,饼里会有" + cake2.eggCount + "个蛋。");
	}
}

 输出结果

例6.4代码

public class Liultd {//类
	static double PI = 3.1415;//在类中定义静态变量
	
	public static void method() {//在类中定义静态方法
		System.out.println("这是静态方法");
	}
	
	public static void main(String[] args) {
		System.out.println(Liultd.PI);//调用静态变量
		Liultd.method();//调用静态方法
	}
}

 输出结果

例6.5代码

public class Liulte {//顾客类
	static int count = 0;//共享的属性:人数
	String name;//名称属性
	public Liulte(String name) {
		this.name = name;//记录名称
		count++;//人数递增
	}
	
	public static void main(String[] args) {
		Liulte c1 = new Liulte("tom");
		System.out.println("我是第"+Liulte.count + "名顾客,我叫" + c1.name);
		Liulte c2 = new Liulte("张三");
		System.out.println("我是第"+Liulte.count + "名顾客,我叫" + c2.name);
		Liulte c3 = new Liulte("狗蛋儿");
		System.out.println("我是第"+Liulte.count + "名顾客,我叫" + c3.name);
	}
}

输出结果

例6.6代码

public class Liultf {//类
	public static void main(String[] args) {//定义主方法
		for (int i = 0; i < args.length;i++) {//根据参数个数做循环操作
			System.out.println(args[i]);//循环打印参数内容
		}
	}
}

 输出结果

例6.7代码

public class Liultg {//类
	String name;//String成员变量
	int age;
	String sex;//String成员变量
	
	public Liultg() {
	}
	
	public Liultg(String name,int age,String sex) {
		this.name = name;//记录名称
		this.age = age;//记录名称
		this.sex = sex;//记录名称
	}
	
	public static void main(String[] args) {//定义主方法
		Liultg p1 = new Liultg("tom",23,"男");
		Liultg p2 = new Liultg("lily",19,"女");
	}
}

例6.8代码

public class Liulth {//狗
	String name;//名字
	String Color;//颜色
	String vioce;//声音
	
	public Liulth(String name,String color,String vioce) {
		this.name = name;
		this.Color = color;
		this.vioce = vioce;
	}
	
	public void call() {//叫
		System.out.println(vioce);
	}
	
	public static void main(String[] args) {
		Liulth d1 = new Liulth("毛毛","白色","汪汪汪");
		System.out.print(d1.name + "的颜色是" + d1.Color);//访问对象的属性
		System.out.print(",叫起来的声音:");
		d1.call();//访问对象的行为
		
		Liulth d2 = new Liulth("灰灰","灰色","嗷呜~");
		System.out.print(d2.name + "的颜色是" + d2.Color);//访问对象的属性
		System.out.print(",叫起来的声音:");
		d2.call();//访问对象的行为
	}
}

 输出结果

public class Dog {
	/*成员变量
	 * 名字
	 * 性别
	 * 颜色
	 * 叫声*/
	private String a;
	private String b;
	private String c;
	private String d;
	//对外提供的方法getter/setter
	public String getA() {
		return a;
	}
	public void setA(String a) {
		this.a = a;
	}
	public String getB() {
		return b;
	}
	public void setB(String b) {
		this.b = b;
	}
	public String getC() {
		return c;
	}
	public void setC(String c) {
		this.c = c;
	}
	public String getD() {
		return d;
	}
	public void setD(String d) {
		this.d = d;
	}
 
	//成员方法
 
	public String intcoduce() {
		//System.out.println( b + "狗" + a + "是" + c +"色的");
		return  b + "狗" + a + "是" + c +"色的";
	}
	public String bark() {
		return b + "狗" + a + "正在" + d + "叫";
	}
 
	//构造方法
 
	public  Dog() {
		
	}
	public  Dog(String b, String a, String c) {
		this.b = b; 
		 this.a = a;
		 this.c = c;
	}
	public Dog(String b, String a, String c, String d) {
		this.b = b;
		this.a = a;
		this.c = c;
		this.d = d;
	}
}

 

public class Dog2 {   //新建类
 
	public static void main(String[] args) { //主函数
		// TODO Auto-generated method stub
		Dog a1 = new Dog("母", "阿花", "黄");//创建第一个对象
		String intcoduce = a1.intcoduce();//调用方法
		System.out.println(intcoduce);//输出调用的方法
		Dog a2 = new Dog("公","小海","绿色","汪汪汪");//创建第二个对象
		String bark = a2.bark();//调用第二个方法
		System.out.println(bark);//输出
	}
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值