programmingwithobjectivec学习笔记(一)

Defining Classes 

Classes Are Blueprints for Objects 

类是对象的模板

In Objective-C, the class interface specifies exactly how a given type of object is intended to be used by other objects. In other words, it defines the public interface between instances of the class and the outside world.

定义interface就是做个连接类和外部的部件

类的interface就是这个类的定义

别人用这个interface来使用你定义好的类

interface一般写在.h里面给其他的文件引用

类的定义和interface的定义是绑定在一起的


Mutability Determines Whether a Represented Value Can Be Changed 

可变和不可变的值

在OBJ-c中 所有basic类型 如NSString或者NSNumber等对象都是不可变的,如果你想表现其他

Some classes define objects that areimmutable.  in Objective-C, all basicNSStringandNSNumberobjects are immutable.  

一些对象的值是不可变的,在OBJECT-c里 所有basic类型如nsstring和nsnumber都是不可变的,类似C语言中的const


Some immutable classes also offer amutableversion. 

you can usean instance of theNSMutableStringclass. Instances of this class behave just likeNSStringobjects 

一些对象的值是可变的,比如继承于NSString的NSMutableString就是可变的对象,可以对其值进行操作·


Classes Inherit from Other Classes 

一个类继承与其他类 

All of the functionality provided by NSString is available in NSMutableString 

比如nsMutableString继承于NSString,NSMutableString可以使用所有NSString的函数


The Root Class Provides Base Functionality 


根类NSObject提供了一些基本功能


The Interface for a Class Defines Expected Interactions 

an object should be designed tohide the details of its internal implementation. 

一个类的定义是为了隐藏他的内部实现


In Objective-C, the interface and implementation are usuallyplaced in separate files so that you only need to make the interface public. 


Basic Syntax 

基本语法

@interface SimpleClass : NSObject
//定义内容
@end
上面定义了一个名为SimpleClass的类继承于NSObject


Properties Control Access to an Object’s Values 

熟悉控制对象值的访问

定义属性

@interface Person : NSObject
@property NSString *firstName;//属性firstName(*代表是个C指针)
@property NSString *lastName;//属性lastName(*代表是个C指针)

           
           
@property NSNumber *yearOfBirth;
@property int yearOfBirth;	//int类型的属性

@end 

Property Attributes Indicate Data Accessibility and Storage Considerations 

属性的访问及存储权限


@interface Person : NSObject
@property (readonly) NSString *firstName;//只读的属性
@property (readonly) NSString *lastName;//只读的属性
@end

Method Declarations Indicate the Messages an Object Can Receive 


发消息=调函数 metod就是传递messages给Object

object中的方法如下

- (void)someMethod;

The minus sign (-) at the front of the method name indicates that it is an instance method, which can be calledon any instance of the class.  

(-)代表此函数是一个实例函数,能被任何本类的实例调用,(后面会说到+代表的类函数,类似静态方法)


Methods Can Take Parameters 

方法使用参数

- (void)someMethodWithValue:(SomeType)value;

- (void)someMethodWithFirstValue:(SomeType)value1 secondValue:(AnotherType)value2; 

someMethodWithFirstValue 是函数名(写法是规范)

SomeType 参数1的类型
value1 参数1的形参
AnotherType 参数2的类型
value2 参数2的形参

这里没有secondValue什么事情
secondValue就是类似多态的写法

Class Names Must Be Unique 

类名必须唯一

The Implementation of a Class Provides Its Internal Behavior 

一个类的实现(Implementation)提供了它的内部行为

As stated earlier, the interface for a class is usually placed inside a dedicated file, often referred to as a headerfile, which generally has the filename extension .h. You write the implementation for an Objective-C classinside a source code file with the extension .m

一般一个类的定义(interface)定义在一个.h文件中,一个类的实现(implementation)在一个.m文件中定义

Basic Syntax 

Implementing Methods 


For a simple class interface with one method, like this:

the implementation might look like this:

XYZPerson.h文件定义XYZPerson类

@interface XYZPerson : NSObject
- (void)sayHello;
@end

XYZPerson.m文件实现XYZPerson类
#import "XYZPerson.h"
@implementation XYZPerson
- (void)sayHello {
    NSLog(@"Hello, World!");
}

@end 

Objective-C Classes Are also Objects 

obj-c类也是一个对象

The typical use for a class method is as a factory method

类也可以有工厂方法,不用实例化类就可以调用

使用(+)符号代表工厂方法,与前面提到的(-)相对应

The NSString class 

+ (id)string;
  + (id)stringWithString:(NSString *)aString;
  + (id)stringWithFormat:(NSString *)format, ...;
+ (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc
  error:(NSError **)error;
  + (id)stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值