您的位置:首页 > 职场人生

黑马程序员——JAVA中的面向对象

2013-01-17 20:43 423 查看
----------- android培训java培训、java学习型技术博客、期待与您交流! ------------

1.对象和类的概念

在程序中操作对象是类的一个实例。

创建一个对象:Hello obj=new Hello();

调用方法:obj.showString()

类是对象的实例化,相当于一个模板

2.比较面向过程的思想和面向对象的思想

面向过程的思想:由过程、步骤、函数组成,以过程为核心;

面向对象的思想:以对象为中心,先开发类,得到对象,通过对象之间相互通信实现功能。

面向过程是先有算法,后有数据结构。

面向对象是先有数据结构,然后再有算法。

3.面向对象(class)

public class AnimalText {

public void eat(){

System.out.println("吃饭");

}

public static void main(String[] args) {

Text t=new Text();

t.eat();

Dog dog=new Dog();

Dog dog1=new Dog("汪汪");

}

}

class Dog{

String say;

Dog(){

System.out.println("狗吃骨头");

}

Dog(String say){

this();

this.say=say;

System.out.print("狗还会叫"+say);

}

}

(一)引用的概念

通过new一个对象,根据这个对象调用对象里的方法的行为叫引用;

如: Text t=new Text();

t.eat();

(二)构造方法的概念

构造方法是当用类生成对象时,系统在生成对象的过程中利用的方法。

注意:构造方法在生成对象的时候会被调用,但并不是构造方法生成了对象。

构造方法没有返回值。格式为:public 方法名。

构造方法的方法名与类名相同。

构造方法是在对象生成的过程中自动调用,不可能利用指令去调用。

在一个对象的生成周期中构造方法只用一次,一旦这个对象生成,那么这个构造方法失效。

例如:

class Dog{

String say;

Dog(){ //狗的构造方法

System.out.println("狗吃骨头");

}

Dog(String say){

this.say=say;

System.out.print("狗还会叫"+say);

}

}

4.方法重载

构造方法重载

在类中可以构造多个构造方法,但多个构造方法的参数表一定不同,参数顺序不同即属于不同的构造方法:即重载

Dog(){ //狗的构造方法

System.out.println("狗吃骨头");

}

Dog(String say){

this.say=say;

System.out.print("狗还会叫"+say);

}

如果我们未给系统提供一个构造方法,那么系统会自动提供一个为空的构造方法。

5.this

在构造方法中,this表示本类的其他构造方法:

class Dog{

String say;

Dog(){ //构造方法Dog

System.out.println("狗吃骨头");

}

Dog(String say){

this(); // this代表是Dog()的方法

this.say=say;

System.out.print("狗还会叫"+say);

}

}

如果调用Dog(String say)则为this(String say)。

特别注意:用this调用其他构造方法时,this必须为第一条语句,然后才是其他语句。

This表示当前对象

Dog(String say){

this(); // this代表是Dog()的方法

this.say=say; //表示当前String的say

System.out.print("狗还会叫"+say);

}

此时打印的是实例变量,而非局部变量,即定义在类中而非方法中的变量。

This.say表示实例变量。

谁调用this.say那么谁即为当前(this)对象的Dog方法。

6.封装:

使对象的属性尽可能私有,对象的方法尽可能的公开。用private表示此成员属性为该类的私有属性。

7.面向对象高级: 修饰符:static

在类中,用static声明的成员变量为静态变量,它为该类的公用变量。第一次使用时被初始化,对于所有的类来说,static变量只有一份。用static声明的变量方法为静态变量,在静态方法中不可访问非静态变量。可以通过对象的引用或类名直接访问静态成员。

public class AnimalText {

private static int sid=0;

private String name;

int id;

AnimalText(String name){

this.name=name;

id=sid++;

}

public void info(){

System.out.println(" name "+name+" id "+id);

}

public static void main(String[] args) {

AnimalText.sid=100;

AnimalText animal=new AnimalText("mimi");

AnimalText animal1=new AnimalText("pipi");

animal.info();

animal1.info();

}

}

8.private default protected public

private :本类访问;

default :表示默认,不仅本类访问,而且是同包可见。

Protected:同包可见+不同包的子类可见

Public :表示所有的地方均可见

9.extends(继承)

父类的非私有化属性和方法可以默认继承到子类。

Class Son extends Father{

}

如果父类中的私有方法被子类调用的话,则编译报错。

父类的构造方法子类不可以继承,更不存在覆盖的问题。(非构造方法可以)

如果子类访问父类的构造方法,则在编译的时候提示访问不到该方法。

JAVA中不允许多继承,一个类有且只有一个父类(单继承)。

JAVA的数据结构为树型结构,而非网状。(JAVA通过接口和内部类实现多继承)

10.overwrite(重写)

子类可以根据需要重写父类的方法,,重写时方法名一定相同,且不能使用比被重写又更严格的限制;

public abstract class Text {

public static void main(String[] args) {

monkey m=new monkey("香蕉");

goalmonkey gm=new goalmonkey("香蕉 ","金色");

gm.eating();

}

}

class monkey{

String eat;

monkey(String eat){

this.eat=eat;

}

public void eating(){

System.out.print(eat);

}

}

class goalmonkey extends monkey{

String eat;

String furcolor;

goalmonkey(String eat,String furcolor){

super(eat);

this.eat=eat;

this.furcolor=furcolor;

}

public void eating(){ //重写父类eating方法

System.out.print("吃:"+eat+" 毛色:"+furcolor);

}

}

11.final

final可以修饰类、属性、方法。

当用final修饰类的时候,此类不可被继承,即final类没有子类。这样可以用final保证用户调用时动作的一致性,可以防止子类覆盖情况的发生。

当利用final修饰一个属性(变量)的时候,此时的属性成为常量。

JAVA利用final定义常量(注意在JAVA命名规范中常量需要全部字母都大写):

Final int AGE=10;

常量的地址不可改变,但在地址中保存的值(即对象的属性)是可以改变的。

Final可以配合static使用。

public class Text {

static final int id=100;

public static void main(String[] args) {

System.out.print(Text.id);

}

}

在JAVA中利用public static final的组合方式对常量进行标识(固定格式)对于在构造方法中利用final进行赋值的时候,此时在构造之前系统设置的默认值相对于构造方法失效。

常量(这里的常量指的是实例常量:即成员变量)赋值:

①在初始化的时候通过显式声明赋值。Final int x=3;

②在构造的时候赋值。

局部变量可以随时赋值。

利用final定义方法:这样的方法为一个不可覆盖的方法。

Public final void print(){};

为了保证方法的一致性(即不被改变),可将方法用final定义。如果在父类中有final定义的方法,那么在子类中继承同一个方法。

如果一个方法前有修饰词private或static,则系统会自动在前面加上final。即private和static方法默认均为final方法。

注:final并不涉及继承,继承取决于类的修饰符是否为private、default、protected还是public。也就是说,是否继承取决于这个方法对于子类是否可见。

12.object

如果在类的声明中没有使用关键字extends指明基类,则默认基类为object类,即父类;object类中代表: toString 、 equals

(1)toString():是利用字符串来表示对象。

当我们直接打印定义的对象的时候,隐含的是打印toString()的返回值。

可以通过子类作为一个toString()来覆盖父类的toString()。

以取得我们想得到的表现形式,即当我们想利用一个自定义的方式描述对象的时候,我们应该覆盖toString()。

(2)equal

两个对象的实例化虽然相同但是它的内存地址不同,所以要调用equals方法,同时也要重写equals方法;

如下例子:

public class Text {

public static void main(String[] args) {

Cat c1=new Cat("bule",22,12);

Cat c2=new Cat("bule",22,12);

System.out.println(c1.equals(c2));

}

}

class Cat{

String color;

int height;

int weight;

public Cat(String colo,int height,int weight){

this.color=color;

this.height=height;

this.weight=weight;

}

public boolean equals(Object obj){//重写equals方法

if(obj==null) return false;

else

{

if(obj instanceof Cat){

Cat c=(Cat)obj;//强制转换

if(c.color==this.color && c.height==this.height && c.weight==this.weight){

return true;

}

}

}

return false;

}

}

13 Abstract(抽象类)

如果将一个类设置为abstract,则此类必须被继承使用,此类不可生成对象,必须被继承使用。

Abstract可以将子类的共性最大限度的抽取出来,放在父类中,以提高程序的简洁性。

Abstract虽然不能生成对象,但是可以声明,作为编译时类型,但不能作为运行时类型。

Final和abstract永远不会同时出现。

当abstract用于修饰方法时,此时该方法为抽象方法,此时方法不需要实现,实现留给子类覆盖,子类覆盖该方法之后方法才能够生效。

注意比较:

private void print(){};此语句表示方法的空实现。

Abstract void print(); 此语句表示方法的抽象,无实现。

如果一个类中有一个抽象方法,那么这个类一定为一个抽象类。

反之,如果一个类为抽象类,那么其中可能有非抽象的方法。

如果让一个非抽象类继承一个含抽象方法的抽象类,则编译时会发生错误。因为当一个非抽象类继承一个抽象方法的时候,本着只有一个类中有一个抽象方法,那么这个类必须为抽象类的原则。这个类必须为抽象类,这与此类为非抽象冲突,所以报错。

所以子类的方法必须覆盖父类的抽象方法。方法才能够起作用。

Abstract和static不能放在一起,否则便会出现错误。(这是因为static不可被覆盖,而abstract为了生效必须被覆盖。)

例:

abstract class Animal {

public abstract void eat(); //定义一个抽象方法

public static void main(String args[]){

cat c=new cat();

c.eat();

}

}

class cat extends Animal{

public void eat(){

System.out.print("cat");//子类中必须重写抽象方法

}

}

14.多态

多态又叫动态绑定,是在执行期间判断所引用的实际类型,根据其类型引用方法;

其中包含:继承、重载、重写;

例子:

public abstract class Text {

public static void main(String[] args) {

Cat c=new Cat("波斯猫");

c.enjoy();

Dog d=new Dog("哈皮狗 ");

d.enjoy();

lady l=new lady("小红 ", c);

l.enjoy();

}

}

abstract class Animal {

String name;

Animal(String name){

this.name=name;

}

public abstract void enjoy();

}

class Dog extends Animal{

Dog(String name) {

super(name);

}

public void enjoy(){

System.out.print("狗叫");

}

}

class Cat extends Animal{

Cat(String name) {

super(name);

}

public void enjoy(){

System.out.print("猫叫");

}

}

class lady{

String name;

Animal pet;

lady(String name,Animal pet){

this.name=name;

this.pet=pet;

}

public void enjoy(){

System.out.print("我的宠物");

}

}

多态使得程序更加灵活;

16.接口

接口是一种特殊的抽象类。

如:

interface IA{

}

public interface:公开接口

与类相似,一个文件只能有一个public接口,且与文件名相同。

在一个文件中不可同时定义一个public接口和一个public类。

一个接口中,所有方法为公开、抽象方法;所有的属性都是公开、静态、常量。

一个类实现一个接口的格式:

class IAImple implements IA{

};

一个类实现接口,相当于它继承一个抽象类。

类必须实现接口中的方法,否则其为一抽象类。

实现中接口和类相同。

接口中可不写public,但在子类中实现接口的过程中public不可省。

(如果剩去public则在编译的时候提示出错:对象无法从接口中实现方法。)

注:

① 一个类除继承另外一个类,还可以实现接口;

class IAImpl extends java.util.Arrylist implement IA{}

继承类 实现接口

这样可以实现变相的多继承。

② 一个类只能继承另外一个类,但是它可以继承多个接口,中间用“,”隔开。

Implements IA,IB

所谓实现一个接口,就是指实现接口中的方法。

③ 接口和接口之间可以定义继承关系,并且接口之间允许实现多继承。

例:interface IC extends IA,IB{};

接口也可以用于定义对象

IA I=new IAImpl();

实现的类从父类和接口继承的都可做运行时类型。

IAImple extends A implement IA,IB

IB I=new IAImple();

I instance of IAImple;

I instance of A;

I instance of IA;

I instance of IB;

返回的结果均为true.

public class AnimalText {

public static void main(String[] args) {

Animal animal=new Dog();

animal.eat();

animal.singer();

}

}

interface Animal{

public void singer();

public void eat();

}

class Dog implements Animal{

@Override

public void singer() {

System.out.print("唱歌");

}

@Override

public void eat() {

System.out.print("吃饭");

}

}

在接口中实现多继承

public class AnimalText {

public static void main(String[] args) {

Animal a=new Dog();//实例化Dog

a.enjoy();

value v=new Dog();

v.valuable();

protect p=(protect)v;//对象转型

p.protectable();

}

}

abstract class Animal{ //定义一个抽象类Animal

public abstract void enjoy() ;

}

interface protect{ //定义一个接口

public void protectable();

}

interface value{

public void valuable();

}

interface hen extends value{ //定义一个继承hen的接口(实现接口对接口)

public void print();

}

class Dog extends Animal implements protect,value{//通过实现多继承

@Override

public void valuable() { // 实现接口

System.out.print("50元");

}

@Override

public void protectable() {// 实现接口

System.out.print("不受保护");

}

public void enjoy(){ //重写抽象方法

System.out.print("动物");

}

}

class Hen implements hen{//实现接口对接口

public void print(){

System.out.print("好吃");

}

public void valuable(){

System.out.print("11");

}

}

----------- android培训java培训、java学习型技术博客、期待与您交流! ------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: