Home Database Mysql Tutorial oc学习笔记2 点语法

oc学习笔记2 点语法

Jun 07, 2016 pm 03:00 PM
Dog ma code study document notes grammar

先来几段代码,Dog类的.h .m文件 和main.m dog.h #import Foundation/Foundation.h@interface Dog : NSObject{ int _ID; NSString *_name; int _age; float _price;}//凡是用initXXX开头的都是构造函数//init方法实际上没有什么特别的,只是遵循命名约定的普

先来几段代码,Dog类的.h .m文件 和main.m

dog.h

#import <foundation>

@interface Dog : NSObject
{
    int _ID;
    NSString *_name;
    int _age;
    float _price;
}
//凡是用initXXX开头的都是构造函数
//init方法实际上没有什么特别的,只是遵循命名约定的普通方法
-(id)init;
-(id)initWithID:(int)newID;
-(id)initWithID:(int)newID andAge:(int)newAge;
-(id)initWithID:(int)newID andAge:(int)newAge andPrice:(int)newPrice;
@property int ID;
@property NSString *name;
@property int age;
@property float price;
@end</foundation>
Copy after login

dog.m

#import "Dog.h"

@implementation Dog
-(id)init
{
    return [self initWithID:1001];

}
-(id)initWithID:(int)newID
{
    return [self initWithID:newID andAge:20];
}
-(id)initWithID:(int)newID andAge:(int)newAge
{
    return [self initWithID:newID andAge:newAge andPrice:80.0];
}
-(id)initWithID:(int)newID andAge:(int)newAge andPrice:(int)newPrice
{
    self=[super init];
    if(self){
        _ID=newID;
        _age=newAge;
        _price=newPrice;
    }
    return self;
}

@synthesize ID=_ID;
@synthesize name=_name;
@synthesize age=_age;
@synthesize price=_price;

@end
Copy after login

main.m

#import <foundation>
#import "Dog.h"
#import "NSString+ReverseString.h"
#import "Person.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        Dog *dog1=[[Dog alloc ] init];
        dog1.name=@"syj";
        NSLog(@"%@",[dog1 name]);
        [dog1 setName:@"ldusyj"];
        NSLog(@"%@",dog1.name);
    }
    return 0;
}
</foundation>
Copy after login


先看一下:

@property

@property int age;

等同于  -setAge:(int)newAge;

 -(void)age;

@synthesize

@synthesize age=_age;

等同于:  -(void)setAge:(int)newAge{

age=newAge;

} 

-(int)age{

return age;

}

在看一下点:

1.

dog1.name=@"syj";
Copy after login
点放到左边相当于调用其对应的set方法。

等同

[dog1 setName:@"syj"];
Copy after login
2.

NSLog(@"%@",dog1.name);
Copy after login
NSLog(@"%@",[dog1 name]);
Copy after login
这两种情况是相同的效果。相当于get方法。


注意:

-(void)setAge:(int)newAge 
{ 
    NSLog(@"调用了setAge方法:%i",newAge); 
    self.age = newAge; 
}
Copy after login


不能在setAge:方法中使用self.age  =  newAge,相当于在setAge:方法中调用[self  setAge:newAge ], 出现死循环

-(int)age 
{ 
    NSLog(@"调用了age方法:%i",_age); 
    return self.age; 
}
Copy after login

不能在age方法中使用return  self.age, 相当于return  [self age];, 出现了死循环


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to delete Xiaohongshu notes How to delete Xiaohongshu notes Mar 21, 2024 pm 08:12 PM

How to delete Xiaohongshu notes? Notes can be edited in the Xiaohongshu APP. Most users don’t know how to delete Xiaohongshu notes. Next, the editor brings users pictures and texts on how to delete Xiaohongshu notes. Tutorial, interested users come and take a look! Xiaohongshu usage tutorial How to delete Xiaohongshu notes 1. First open the Xiaohongshu APP and enter the main page, select [Me] in the lower right corner to enter the special area; 2. Then in the My area, click on the note page shown in the picture below , select the note you want to delete; 3. Enter the note page, click [three dots] in the upper right corner; 4. Finally, the function bar will expand at the bottom, click [Delete] to complete.

What to do if the 0x80004005 error code appears. The editor will teach you how to solve the 0x80004005 error code. What to do if the 0x80004005 error code appears. The editor will teach you how to solve the 0x80004005 error code. Mar 21, 2024 pm 09:17 PM

When deleting or decompressing a folder on your computer, sometimes a prompt dialog box &quot;Error 0x80004005: Unspecified Error&quot; will pop up. How should you solve this situation? There are actually many reasons why the error code 0x80004005 is prompted, but most of them are caused by viruses. We can re-register the dll to solve the problem. Below, the editor will explain to you the experience of handling the 0x80004005 error code. Some users are prompted with error code 0X80004005 when using their computers. The 0x80004005 error is mainly caused by the computer not correctly registering certain dynamic link library files, or by a firewall that does not allow HTTPS connections between the computer and the Internet. So how about

What should I do if the notes I posted on Xiaohongshu are missing? What's the reason why the notes it just sent can't be found? What should I do if the notes I posted on Xiaohongshu are missing? What's the reason why the notes it just sent can't be found? Mar 21, 2024 pm 09:30 PM

As a Xiaohongshu user, we have all encountered the situation where published notes suddenly disappeared, which is undoubtedly confusing and worrying. In this case, what should we do? This article will focus on the topic of &quot;What to do if the notes published by Xiaohongshu are missing&quot; and give you a detailed answer. 1. What should I do if the notes published by Xiaohongshu are missing? First, don't panic. If you find that your notes are missing, staying calm is key and don't panic. This may be caused by platform system failure or operational errors. Checking release records is easy. Just open the Xiaohongshu App and click &quot;Me&quot; → &quot;Publish&quot; → &quot;All Publications&quot; to view your own publishing records. Here you can easily find previously published notes. 3.Repost. If found

What is hiberfil.sys file? Can hiberfil.sys be deleted? What is hiberfil.sys file? Can hiberfil.sys be deleted? Mar 15, 2024 am 09:49 AM

Recently, many netizens have asked the editor, what is the file hiberfil.sys? Can hiberfil.sys take up a lot of C drive space and be deleted? The editor can tell you that the hiberfil.sys file can be deleted. Let’s take a look at the details below. hiberfil.sys is a hidden file in the Windows system and also a system hibernation file. It is usually stored in the root directory of the C drive, and its size is equivalent to the size of the system's installed memory. This file is used when the computer is hibernated and contains the memory data of the current system so that it can be quickly restored to the previous state during recovery. Since its size is equal to the memory capacity, it may take up a larger amount of hard drive space. hiber

How to use Copilot to generate code How to use Copilot to generate code Mar 23, 2024 am 10:41 AM

As a programmer, I get excited about tools that simplify the coding experience. With the help of artificial intelligence tools, we can generate demo code and make necessary modifications as per the requirement. The newly introduced Copilot tool in Visual Studio Code allows us to create AI-generated code with natural language chat interactions. By explaining functionality, we can better understand the meaning of existing code. How to use Copilot to generate code? To get started, we first need to get the latest PowerPlatformTools extension. To achieve this, you need to go to the extension page, search for &quot;PowerPlatformTool&quot; and click the Install button

Detailed explanation of the role of .ibd files in MySQL and related precautions Detailed explanation of the role of .ibd files in MySQL and related precautions Mar 15, 2024 am 08:00 AM

Detailed explanation of the role of .ibd files in MySQL and related precautions MySQL is a popular relational database management system, and the data in the database is stored in different files. Among them, the .ibd file is a data file in the InnoDB storage engine, used to store data and indexes in tables. This article will provide a detailed analysis of the role of the .ibd file in MySQL and provide relevant code examples to help readers better understand. 1. The role of .ibd files: storing data: .ibd files are InnoDB storage

Let's learn how to input the root number in Word together Let's learn how to input the root number in Word together Mar 19, 2024 pm 08:52 PM

When editing text content in Word, you sometimes need to enter formula symbols. Some guys don’t know how to input the root number in Word, so Xiaomian asked me to share with my friends a tutorial on how to input the root number in Word. Hope it helps my friends. First, open the Word software on your computer, then open the file you want to edit, and move the cursor to the location where you need to insert the root sign, refer to the picture example below. 2. Select [Insert], and then select [Formula] in the symbol. As shown in the red circle in the picture below: 3. Then select [Insert New Formula] below. As shown in the red circle in the picture below: 4. Select [Radical Formula], and then select the appropriate root sign. As shown in the red circle in the picture below:

Create and run Linux ".a" files Create and run Linux ".a" files Mar 20, 2024 pm 04:46 PM

Working with files in the Linux operating system requires the use of various commands and techniques that enable developers to efficiently create and execute files, code, programs, scripts, and other things. In the Linux environment, files with the extension &quot;.a&quot; have great importance as static libraries. These libraries play an important role in software development, allowing developers to efficiently manage and share common functionality across multiple programs. For effective software development in a Linux environment, it is crucial to understand how to create and run &quot;.a&quot; files. This article will introduce how to comprehensively install and configure the Linux &quot;.a&quot; file. Let's explore the definition, purpose, structure, and methods of creating and executing the Linux &quot;.a&quot; file. What is L

See all articles