第六章总结1

面向对象概述
对象
世间万物都是对象;通常划分为两个部分:动态属性和静态属性动态属性就是对象可执行的动作静态属性就是不能动的部分,这个部分被称为属性,任何对象都会具备其自身属性


类,是同一类事物的统称类实质上就是封装对象属性和行为的载体类是对象的设计图面向对象具有封装性,继承性和多态性的特点

封装
是面向对象编程的核心,将对象的属性和行为封装起来,载体是类。封装保证了类内部数据结构的完整性,使用类的用户不能轻易的直接操作类的数据结构,只能执行类允许公开的数据,避免了外部操作对内部数据的影响,提高了系统的可维护性。

继承
继承性主要利用特定对象的共有属性,子类可以继承父类所有的属性,子类还可以衍生子类,但最末尾的类依然能继承父类所有的属性父类和子类存在一种关系,一个类在继承中它即可以是其他类的父类,为其他类提供属性和行为,也可以是其他类的子类。例如:三角形是图形类的子类也是等边三角形的父类。

多态
其实就是父类对象应用于子类的特征就是多态,多态就是继承父类的所有但又会有不属于父类也不同于其他子类的属性就相当于它的个性。

封装
成员变量和成员方法就是静态属性和动态属性;
1.

package duilian;
 
public class book {//类
public String name;//string成员变量
public String getName() {//getter方法
	return name;
}
	public  void setName(String name) {//name的setter方法
		this.name=name;//赋值
	}
 
}

 

 2.

package duilian;
 
public class changedemo {
public static int[] exchange(int[]arr) {
	int tmp=arr[0];///局部变量
	arr[0]=arr[1];//第一个元素赋值给第二个元素
	arr[1]=tmp;//第二个元素值为tmp
	return arr;
}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int arr[]= {17,19};
		System.out.println("第一个值="+arr[0]+"第二个值="+arr[1]);
		arr=exchange(arr);
		System.out.println("第一个值="+arr[0]+"第二个值="+arr[1]);
	}
}

 3.

package duilian;
 
public class eggcake {//个数
	int eggcount;
	public eggcake(int eggcount) {//构造方法
		this.eggcount=eggcount;//属性
	}
	
	public eggcake() {//构造方法
		this(1);
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
	eggcake cake1=new eggcake();
		System.out.println("顾客不要求加蛋的是数量,饼里有"+cake1.eggcount+"个蛋。");
	eggcake cake2=new eggcake(2);	
		System.out.println("顾客要加两个蛋,饼有"+cake2.eggcount+"个蛋。");	
	}
}

4.

package duilian;
 
public class staticdemo {
static double PI=3.1415926;//静态变量
static final int i=3;//静态常量
static void method() {//在类中定义静态方法
	System.out.println("这是静态方法");//打印
	
}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(staticdemo.PI);//调用静态变量
		System.out.println(staticdemo.i);
		staticdemo.method();
	}//调用静态方法
 
}

 

 5.

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

7.

package duilian;
 
public class people {
		String name;//名字
		int age;//年龄
		String sex;
		
		public people() {//构造方法
		}
		
		public people(String name,int age,String sex) {
			this.name=name;//记录名称
			this.age=age;
			this.sex=sex;
		}
		
		public static void main(String[] args) {
			// TODO Auto-generated method stub
			people p1=new people("tom",23,"男");
			people p2=new people("liy",19,"女");
			
			
			
			
	}
 
}

 8.

package a;
 
public class b {		
	String name;//名字
	String color;//颜色
	String vioce;//叫声
	
	public b(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) {
		b d1=new  b("毛毛","白色","汪汪汪");
		System.out.print(d1.name+"的颜色是"+d1.color);//访问对象的属性
		System.out.print("叫起来的声音:");
		d1.call();//访问对象的行为
		b d2=new b("灰灰","灰色","嗷呜~");
		System.out.print(d2.name+"的颜色是"+d2.color);
		System.out.print("叫起来的声音:");
		d2.call();
		
	}
 
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值