隐藏"Swifter"的信息道路? [英] Information Hiding the "Swifter" way?

查看:98
本文介绍了隐藏"Swifter"的信息道路?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对面向对象的设计原理和Swift有疑问.我对Java非常熟悉,而且我目前正在参加一项大胆的课程,以初次接触Swift.

I have a question regarding object oriented design principles and Swift. I am pretty familiar with Java and I am currently taking a udacity course to get a first hands on in Swift.

在Java社区(基本上在OOP之后的每个社区中),很常见的方法是使用信息隐藏技术,例如将数据隐藏或封装在类中以确保无法从外部进行操作.一个共同的原则是将类的所有属性声明为私有,并使用getter来获取属性的值,并使用setter来进行操作.

In the Java community (basically in every community that follows OOP) it is very common to use information hiding techniques such as hiding or encapsulating data within classes to make sure it cannot be manipulated from outside. A common principle is to declare all attributes of a class as being private and use getters for retrieving an attribute's value and setters for manipulation.

在编写课程的一部分时,我试图遵循这种方法,看起来像这样:

I tried to follow this approach when writing a class that was part of the course and it looks like this:

//
//  RecordedAudio.swift
//  Pitch Perfect
//

import Foundation

class RecordedAudio: NSObject {
    private let filePathUrl: NSURL!
    private let title: String?

    init(filePathUrl: NSURL, title: String?)
    {
        self.filePathUrl = filePathUrl
        self.title = title
    }

    func getFilePathUrl() -> NSURL
    {
        return filePathUrl
    }

    func getTitle() -> String
    {
        if title != nil
        {
            return title!
        }
        else
        {
            return "none"
        }
    }
}

代码可以正常工作,并且无法从类外部访问我的私有属性,而这正是我想要实现的行为.但是,我想到了以下问题:

The code works and my private attributes cannot be accessed from outside my class, which is exactly the behavior I wanted to achieve. However, the following questions came to my mind:

1.)课程讲师决定将属性的访问控制级别保留为默认的内部",而不使用getter/setter,而是直接从外部访问属性.关于为什么开发人员可能会迅速做到这一点的任何想法?我的方法不够迅速"吗?

1.) The course instructor decided to leave the attributes' access control level at the default "internal" and not use getters/setters but rather access the attributes directly from outside. Any thoughts on why developers might do that in swift? Is my approach not "swift" enough???

2.)结论:在编写您自己的类时,是否有一种迅捷"的方法来实现封装? swift的本机技术是什么,以实现我想要的信息隐藏?

2.) In conclusion: Is there a "swifter" way to implement encapsulation when writing your own class? What are swift's native techniques to achieve the information hiding I am aiming for?

推荐答案

您可以限制外部属性的操作,方法是将属性public标记为可读取,将private标记为可写入,如

You can restrict external property manipulation, by marking the property public for reading and private for writing, as described in the documentation:

class RecordedAudio: NSObject {

    public private(set) let filePathUrl: NSURL!
    public private(set) let title: String?

    init(filePathUrl: NSURL, title: String?) {
        self.filePathUrl = filePathUrl
        self.title = title
    }

}

// in another file

let audio = RecordedAudio(filePathUrl: myUrl, title: myTitle)

let url = audio.filePathUrl // works, returns the url
audio.filePathUrl = newUrl // doesn't compile

这篇关于隐藏"Swifter"的信息道路?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆