十分钟看完面向对象

                                    前引:在java面向对象的学习中真的是难道无数人,本人也是常常被次困扰,面向对象主要在于对对象的理解,下面我们来对此进行讲解。

面向对象特征:封装,继承,多态,抽象

     封装:封装:什么是封装?大白话,你在网上买了一个苹果笔记本电脑,你不用在意他的硬件,他怎么生产的,他怎么邮寄到你这的,

要在意的是,它能干什么?他可以打代码,可以装B,你只需要他能实现什么功能,封装到一个方法里,到时候后你再去调用,就像上课老

师说的,狗能叫,鱼能游泳是一个意思。(不用在意他别的属性细节)


public class Student {
public static void main(String[] args) {
Behaviour behaviour = new Behaviour("老王", "男", "Apple笔记本电脑");
behaviour.buy();
}
}

class Behaviour {
private String name;
private String sax;
private String computer;

public Behaviour(String string, String string2, String string3) {
super();
this.name = string;
this.sax = string2;
this.computer = string3;

}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSax() {
return sax;
}

public void setSax(String sax) {
this.sax = sax;
}

public String getComputer() {
return computer;
}

public void setComputer(String computer) {
this.computer = computer;
}

@Override
public String toString() {
return "behaviour [name=" + name + ", sax=" + sax + ", computer=" + computer + "]";
}

public void buy() {
System.out.println(this.toString());
}

}

    继承:们常说,你看老王的儿子和他爸一样有力气,WBQ的儿子一点不像他(没有继承他)前者就是完美的继承,继承了父亲的良好基因,

在上课中老师常说的是,小狗继承了老狗,老狗会叫,小狗也会叫,这就是继承。


public class ex {
public static void main(String[] args) {
Father father = new Father(1000000, "北京二环别墅房1102");
father.say();

San san = new San(1000000, "北京二环别墅房1102" + "(这里面是继承过来)");
san.say();
}

}

class Father {
private int money;
private String house;

public Father(int money, String house) {
super();// 让它成为父类,让别的类继承
this.money = money;
this.house = house;

}

public int getMoney() {
return money;
}

public void setMoney(int money) {
this.money = money;
}

public String getHouse() {
return house;
}

public void setHouse(String house) {
this.house = house;
}

public void say() {
System.out.println("我有" + this.money + "块钱" + "并且" + this.house + "是我的房子");
}
}

class San extends Father {

public San(int money, String house) {// 自动调用父类的构造方法
super(money, house);

}

}

    多态:

(难点)什么是多态 ?举个例子,高三的同学准备迎接高考,正好下一节课是体育课,你们的老师说,下节体育课停了,上我的英语,

(现在一个班的同学每个同学都是一个类)而每个同学对老师说的话做出的反应不同,想考清华北大的同学高兴的手舞足蹈,而不想

学习的学生就不高兴了,而这只是表示了两大类,而其实是每个同学对此做出的反应都不一样,就是每个类做出的反应都不同,这就

是多态。

import java.util.Arrays;

class zong {
public int ix;

public zong(int ix) {
this.ix = ix;
}

public void show() {
System.out.println(this.ix);
}

public void means1() {
System.out.println("means1");
means2();
}

public void means2() {
System.out.println("means2");
}

}

class JNC extends zong {
public JNC(int ix) {
super(ix);
}
public int iy;

public void means1(String a) {
System.out.println("FNC的means1.....");
means2();

}

public void means2() {
System.out.println("means2");
}
}

public class 多态 {
public static void main(String[] args) {
zong w = new zong(0);
w.means1();
JNC jnc = new JNC(0);
jnc.means1("as");
zong q = new JNC(5);
q.show();


}

}

猜你喜欢

转载自www.cnblogs.com/g2vbn/p/9645833.html