面向对象
Java 是一个纯面向对象的语言
1、写程序的思路:
- 一切皆对象
- 谁,做什么
- 抽象 类型=属性+方法
2、目的
- 简化开发
- 代码接近人类语言
- 不会增加效率,反而影响效率
- 代码量会增加
3、抽象
- 把事务的特征和行为抽象出来的过程
4、面向对象的特征:
-
封装
-
继承
-
多态
5、语法
- 一个类如果没显示任何构造方法,这个类有一个默认无参构造方法,如果自己写了一个构造方法,默认无参构造方法将失效。
封装
- 属性外部不能直接访问
- 需要访问属性应该为外部提供公开的行为进行访问
- private :私有的
- public :公开的
1、一个对象被当做字符串使用的时候,会自动调用toString 方法(对象和字符串之间的转换)
public string toString(){}
2、练习1
练习重点 抽象 this的使用
定义一个人类,给出相应的属性
其中有一个配偶属性 patrener
提供一个结婚的方法 marry
结婚符合现实中国法律逻辑
(同性不能结婚)(已经结婚的不能结婚)
package day06.pm;
public class Person {
private String name;
private boolean sex;
private Person patrener;
public Person(String name, boolean sex) {
this.name = name;
this.sex = sex;
patrener=null;
}
public boolean marry(Person partener){
if(this.sex==partener.sex){
System.out.println("同性不能结婚");
return false;
}
if(this.patrener!=null||partener.patrener!=null){
System.out.println("已经结婚的不能结婚");
return false;
}
this.patrener=partener;
partener.patrener=this;
return true;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Person getPatrener() {
return patrener;
}
public void setPatrener(Person patrener) {
this.patrener = patrener;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
}
3、练习2
练习重点 封装
定义一个放int类型的栈类 IntStack int[] data int size
提供入栈,出栈,查看栈的大小的方法 void push(int i){} int pop(){} int getSize(){}
栈特征,先进后出
构造方法可以设置栈的大小
package day06.pm;
public class IntStack {
private int[] data;
private int size;
public IntStack(int max) {
data=new int[max];
size=0;
}
public IntStack() {
this(10);
}
public boolean isFull(){
return size==data.length;
}
public boolean isEmpty(){
return size==0;
}
public void push(int i){
if(isFull()){
throw new RuntimeException("栈已满");
}
data[size++]=i;
}
public int pop(){
if(isEmpty()){
throw new RuntimeException("栈已空");
}
return data[--size];
}
public int getSize(){
return size;
}
}
调用
package day06.pm;
public class Test {
public static void main(String[] args) {
// Person p1=new Person("aaa",true);
// Person p2=new Person("bbb",true);
// Person p3=new Person("ccc",false);
// Person p4=new Person("ddd",true);
//
// p1.marry(p2);
//
// System.out.println(p1.marry(p3));
//
// p3.marry(p4);
// IntStack s=new IntStack(100);
IntStack s=new IntStack();
// s.setSize(6);
s.push(5);
s.push(4);
s.push(1);
s.push(9);
s.push(2);
int size=s.getSize();
for (int i = 0; i < size; i++) {
System.out.println(s.pop());
}
}
}
4、练习3
练习重点 重载
所有方法都可以重载多个(把能想到的都写)
定义一个点类(数学上的点(x,y)) Point
抽象属性
提供一个计算两点之间距离的方法 distance Math.sqrt() Math.pow(num,x)
定义一个圆类Circle
抽象出属性
提供计算面积的方法 area Math.PI
提供判断一个点是否在圆内的方法 contains
package day07;
public class Point {
private double x;
private double y;
public Point(String point){
point=point.substring(1,point.length()-1);
String[] s=point.split(",");
this.x=Double.parseDouble(s[0]);
this.y=Double.parseDouble(s[1]);
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public Point(double x,String s) {
if("x".equals(s)){
this.x=x;
this.y=0.0;
}
if("y".equals(s)){
this.x=0.0;
this.y=x;
}
}
public Point() {
this(0.0,0.0);
}
public double distance(Point other){
return distance(other.x, other.y);
}
public double distance(double x,double y){
return Math.sqrt(Math.pow(this.x-x, 2)+Math.pow(this.y-y, 2));
}
public double distance(){
// return distance(0, 0);
// return distance(new Point());
return distance(new Point(0,0));
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public String toString() {
return "("+x+","+y+")";
}
}
package day07;
public class Circle {
private Point center;
private double r;
public Circle(Point center, double r) {
this.center = center;
this.r = r;
}
//半径为1
public Circle(Point center) {
this(center,1);
}
//圆心在(0,0)点
public Circle(double r) {
this(new Point(),r);
}
//单位圆
public Circle(){
this(0,0,1);
}
public Circle(double x,double y,double r){
this(new Point(x,y),r);
}
public double area(){
return Math.PI*r*r;
}
public boolean contains(Point p){
return p.distance(center)<=r;
}
public boolean contains(double x,double y){
return this.contains(new Point(x,y));
}
public boolean contains(){
return center.distance(0,0)<=r;
}
public void setX(double x){
if(center==null){
return;
}
this.center.setX(x);
}
public void setY(double y){
if(center==null){
return;
}
this.center.setY(y);
}
public Double getX(){
if(center==null){
return null;
}
return center.getX();
}
public Double getY(){
if(center==null){
return null;
}
return center.getY();
}
public Point getCenter() {
return center;
}
public void setCenter(Point center) {
this.center = center;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
}
5、
- String.format(“%05d”,n)格式化字符串
- 可以将n扩充为5位,不够前面加0补充,超过五位按实际情况写。
6、this(参数);
- 调用自己的构造方法
- 注意:只能出现在构造方法的第一行
- 不能循环调用,即至少有一个构造方法里面没有出现this()。
7、例:Person p= new Person("abc","男");
- 堆里分配空间
- 初始化属性
- 调用构造方法
- 赋值操作
- 栈里定义一个Person变量p
- 将堆里的对象的首地址赋值给变量p
8、public static final
修饰的属性是常量
- 公开的任何地方都能访问
- 所有人共享
- 不可变
9、final 不可变
- 类 最终类,不可以被继承
- 属性 属性的值在创建完对象,第一次赋值以后不允许修改
- 变量 初始化后不允许修改栈里的数据,堆里的数据可以被修改
- 方法 方法不可以被重写
- 例:
final Point p=new Point(4,6);
p=new Point(8,4);//可以
final int i=1;
//i=5;//不可以,因为i是被final修饰的基本类型,放在栈中,不可以被修改
10、static 静态的)(用类名调用)
-
使用面向对象无法解释
-
属于类,类加载时就有,所有对象共享
-
属性 所有对象共享的同一个变量
-
方法 工具方法
-
内部类 静态内部类
-
注意:静态的东西不能访问成员的东西
原因是存在的时机,静态的先加载,成员的在创建完对象就存在。
private int i;
//成员属性,在创建对象时分配空间
private static int j;
//静态属性,类加载时分配空间
static{
System.out.println("static{}");
}
//静态代码块,实际就是个静态方法,没有方法名,所以不能调用,而是虚拟机在类加载的时候自动调用一次。
{System.out.println("{}");}
//代码块,创建对象时,自动调用一次
public void f(){
System.out.println("c.f()");
}
//成员方法
public static void fn(){
System.out.println("c.fn()");
}
//静态方法
11、继承 is a (泛化) extends
- 想使用,就必须有is a 的关系
- 如果构造方法第一行没有this(参数),默认相当于有一个super()
- Java中是单继承,一个类只能有一个直接父类
12、多态
- 一种事务,多种形态
对象的多态:
- 把一个对象用它自己或者父类型来引用
- 需要父类型时,可以给它任意子类型的对象
方法的多态:
- 方法的重载
在一个类中,方法名相同,参数不同 - 方法的重写(覆盖)
父子类 方法名相同,参数相同,返回类型相同,访问权限不能更小,抛出异常不能更大。
13、父类引用指向子类对象
- 调用重写的方法 执行子类的方法
- 调用子类自己的方法 需要做强制类型转换 父类的对象不能直接调用子类自己的方法
Animal b=new Bird();
//父类的引用指向一个子类的对象
if(b instanceof Bird){
((Bird)b).sing();
}//判断变量b是否是Bird类型
14、Bird b =new Bird();//Bird extends Animals
- 1、分配父类空间
- 2、分配子类空间
- 3、初始化
- 4、调用构造方法
- 5、赋值
15、修饰符 (访问权限修饰符)
16、abstract 抽象的
- 方法 类
- 有抽象方法的类一定是抽象类
- 抽象类不一定有抽象方法
- 抽象类不能实例化
- 但有构造方法
- 给子类构造方法使用
- 抽象类的最底层子类(普通类)必须实现所有抽象方法
17、类和类之间的关系
- is a 继承 实现
- has a 组合 聚合 关联
- use a 依赖
高内聚低耦合
18、接口 interface
-
实际设计时先有接口再有具体的类等
-
接口是规范
-
作用:1、规范 2、分离调用者和实现者
-
定义的属性: public static final
-
定义的方法: public abstract
-
接口中所有的修饰符都可以省略(默认)
-
接口没有构造方法,不能实例化
-
一个类可以实现多个接口
-
接口可以继承多个接口
public interface I1{
...
}//定义了一个接口
public class C1 implements I1,I2{
...
}//类实现多个接口
public interface I2 extends I1{
...
}//I2接口继承I1接口