饥人谷技术博客

Java面向对象编程——封装和继承

2018-08-30  本文已影响25人  迷路的丸子

面向对象是编程的重点,涉及到类的封装继承抽象多态接口的设计等。其中,封装、继承、多态是面向对象的三大特征。MVC架构中的"Model"部分的设计,实际上就是基于对象的设计,就是面向对象编程。而面向对象编程,是一门较深的学问。面向对象编程的方法可以通过具体实例总结,而在不同的具体环境及需求中,对象设计及方法的应用,是需要长期的实践经验来积淀。通过不断熟练面向对象编程思维,权衡功能以尽可能实现需求,来完善代码,进一步增强代码可读性和简洁性、实用性。因此,“武功秘诀”虽简单,但练就绝非一朝一夕。

本文基于Java面向对象编程,引用了一些实例及相应方法,以尽可能总结Java面向对象编程封装继承的要素。

封装

封装代码实例

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class News {
    private String title;                                         
    private String content;                                       //private限制了外部访问
    
    public News(){}                                               //缺省构造函数

    public News(String title,String content) {                    //构造函数初始化成员变量
        this.title = title;
        this.content = content;
    }

    //可改变title和content,相当于一种setter方法
    //title和content被final修饰时,此方法会报错
    public void read(String fileUrl) throws IOException{          
        try {
            //从Url读取内容
            BufferedReader  reader = new BufferedReader(new FileReader(new File(fileUrl)));
            title = reader.readLine();                            //赋值给title
            content = reader.readLine();                          //赋值给content
        }
        catch (IOException e) {
            System.out.println("新闻读取出错");                     //异常抓取
        }
    }

    public String getTitle() {                                    //getter方法,为title提供可读接口
        return title;
    }

    public String getContent() {                                  //getter方法,为content提供可读接口
        return content;
    }

    //控制如何显示,显示格式,相当于一种getter方法
    public String display() {
        return title + "\n" + content;
    }
}

测试代码

import java.io.IOException;

public class Main {

    public static void main(String[] args) throws IOException {
        News news = new News("abc","blabla");
        System.out.println(news.display());
        news.read("");
        System.out.println(news.display());
    }
}

测试结果

abc
blabla
新闻读取出错

继承

父类代码实例

public class News {
    protected String title;         //可被子类访问
    protected String content;

    //无参构造器
    public News(){}

    // 构造的自由和责任交给用户
    public News(String title, String content) {
        this.title = title;
        this.content = content;
    }

    public String getTitle() {
        return title;
    }

    public String getContent() {
        return content;
    }

    // 控制如何显示
    public String display() {
        return title + "\n" + content;
    }
}

子类代码实例

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class FileNews extends News{
    public FileNews(String title ,String content) {
        super(title, content);                  //调用父类构造函数
    }

    public FileNews() {}                        //默认有个super()

    public void read(String url) throws IOException {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(new File(url)));
            title = reader.readLine();          // 读取title
            reader.readLine();                  // 跳过空行
            content = reader.readLine();        // 读取content
        }
        catch (IOException e) {
            System.out.println("新闻读取出错");  // 异常处理
        }
    }

    public String display() {                   // display()方法的override
        return super.display() + "from 子类";
    }
}

测试代码

import java.io.IOException;

public class Main {

    public static void main(String[] args) throws IOException {
        News news = new News("abc","父类");
        System.out.println(news.display());

        FileNews fileNews = new FileNews("123","子类");
        System.out.println(fileNews.display());
    }
}

测试结果

abc
父类
123:子类
上一篇 下一篇

猜你喜欢

热点阅读