达内---------面向对象基础

诶-_-|||很悲剧的感冒了........难受........今天早点睡了...............

**********************************我是万恶的分隔线*************************************

只有引用类型在不定义数值的情况下为空

面向过程的编程思想

面向对象的编程思想

师法自然

一切皆是对象,按照类别分类(class)

对象:客观世界的每一个事物

:把同一类别的对象归纳总结成类,是概念上的,客观世界中找不到具体的类.

属性:记录了事物的特征(状态),:学号/姓名/班级/年龄(特征)

方法:记录了事物的行为,:考试/学习(行为)

类的语法:

package xx.xx; //0-1

import xx.xx; //0-n

[修饰符] class 类名{ }

在类的{ },可以写属性/方法/构造,不能直接写语句

属性语法:

[修饰符] 属性类型 属性名 [=初始值];

属性如果不赋值,有系统默认值(与数组元素一样),属性必须直接定义在类体的{}

写代码时需要写类,用代码用对象,构造用于创造某个类的对象,每个类都有构造,如果程序员不写构造,系统会提供一个默认无参的构造.如果程序员写了,系统不会提供.

构造语法:

[修饰符] 类名(参数列表) [throws XXException]{}

构造没有返回说法,名字必须是类名.

方法语法:

[修饰符] 返回类型 方法名(参数列表) [throws XXException]{ }

方法如果有返回值的话,必须用return语句.

用面向对象的思想写一个学生类

学生{

特征:id/name/age/sex

行为:study/eat

构造两个:无参/四个参数(与特征对应)

}

package day6.tarena.com; public class Student { private int id; public int getId(){ return id; } public void setId(int id){ if(id>=0){ this.id=id; }else{ System.out.println("学号有误"); } } private String name; public String getName(){ return name; }; public void setName(){}; private int age; //隐藏age,类外看不到age public int getAge(){ return age; } public void setAge(int age){ if(age>2&&age<80){ this.age=age; }else{ System.out.println("年龄有误"); } }//非常量的属性一般都需要封装 String sex; //属性 public Student(int id,String name,int age,String sex){ //如何区分属性id和参数id? this.id=id; //this.id代表属性id,id代表参数 this.name=name; this.setAge(age); this.sex=sex; } //构造 public Student(){ /* id=1; name="zhangsan"; age=12; sex="woman"; */ this(1,"zhangsan",12,"woman"); } public void study(String st){ System.out.println(name+"学习了"+st); } public String eat(){ return "ok"; } //行为 /** * @param args */ // public static void main(String[] args) { // TODO Auto-generated method stub // } }

package day6.tarena.com; public class TestStudent { /** * @param args */ public static void main(String[] args) { Student st=new Student(110,"wu",22,"man");//new+构造,这是对象,st不是对象,而是引用,引用指向对象,无法指向引用 st.setAge(24); st.study("math"); String str=st.eat(); System.out.println(str); System.out.println("***********************************************"); Student st1=new Student(); Student st2=new Student(2,"li",23,"man"); st1.setAge(24); st2.study("math"); String str2=st.eat(); System.out.println(str2); System.out.println("***********************************************"); } }

写一个产品类Product

特征:产品编号/名称/价格/产地

行为:定价/显示产品信息

构造:无参/有参

然后写一个TestProduct,调用测试.

package day6.tarena.com; public class Product { String id; String name; double price; String addr; public Product(){} public Product(String id,String name,double price,String addr){ this.id=id; this.name=name; this.price=price; this.addr=addr; } public void setPrice(double p){ //方法名规则:首字母小写,后面的大写. if(p>0){ price=p; // System.out.println("价格改为"+price); }else{ System.out.println("价格不合理"); } } public void show(){ System.out.println(name+","+price+","+id+","+addr); } }

package day6.tarena.com; public class TestProduct { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Product p =new Product("007","气球",0.5,"中国"); p.setPrice(1); p.show(); } }

this用法:

this代表本类的一个对象,在方法中代表方法的调用者,在构造中代表即将构造的对象.

this.调本类的属性和方法,如果没有重名时,可以省略this.

this()调本类的构造,一般少参的调多参的,必须出现在本类构造的第一行

this不能出现在static部分.

变量和变量的作用范围

属性:

类变量 直接定义在类体{},static修饰,作用范围为本类所有地方

成员变量 直接定义在类体{},没有static修饰, 作用范围为本类所有非static地方

局部变量: 定义在其他位置,作用范围:看定义所在的{}

程序是从主方法开始顺序执行的,即在非主方法中的次序可以随意调用

属性的封装

有些属性不能直接赋值,直接赋值会导致非常错误的属性值.

封装步骤:

1. private 修饰属性,隐藏属性

2. 提供操作属性的方法,一般是读/写方法(getXX/setXX)

3. 在构造中,调用set方法操作属性,不要直接赋值.

package day6.tarena.com; public class TestVar { int i1=1; //成员变量,用于本类非static部分 static int i2=2; //类变量,本类所有地方都可以使用 public void test(){ System.out.println(i1); System.out.println(i2); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub //System.out.println(i1); TestVar p =new TestVar(); p.test(); System.out.println("****************************************************"); System.out.println(i2); int i3=3; //main中 if(i3>1){ int i4=2; //if{}中 }else{ //System.out.println(i4); } //System.out.println(i4); } }

作业:

1. 今天代码和概念理一下.首要的是代码!

2. 编程:

a) 写一个员工类,要求面向对象/封装.

i. 特征:编号/姓名/工资/入职时间

ii. 行为:加薪/辞职

iii. 构造:两个

iv. 考虑下入职时间如何解决?

b) 五子棋继续完成.

判定胜利的算法:

落子后,判定横向的算法:

先找出左边第一个不是该颜色的坐标,再找出右边第一个不是该颜色的坐标

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值