oc学习笔记2 点语法
先来几段代码,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>
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
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>
先看一下:
@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";
等同
[dog1 setName:@"syj"];
NSLog(@"%@",dog1.name);
NSLog(@"%@",[dog1 name]);
注意:
-(void)setAge:(int)newAge { NSLog(@"调用了setAge方法:%i",newAge); self.age = newAge; }
不能在setAge:方法中使用self.age = newAge,相当于在setAge:方法中调用[self setAge:newAge ], 出现死循环
-(int)age { NSLog(@"调用了age方法:%i",_age); return self.age; }
不能在age方法中使用return self.age, 相当于return [self age];, 出现了死循环

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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.

When deleting or decompressing a folder on your computer, sometimes a prompt dialog box "Error 0x80004005: Unspecified Error" 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

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 "What to do if the notes published by Xiaohongshu are missing" 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 "Me" → "Publish" → "All Publications" to view your own publishing records. Here you can easily find previously published notes. 3.Repost. If found

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

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 "PowerPlatformTool" and click the Install button

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

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:

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 ".a" 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 ".a" files. This article will introduce how to comprehensively install and configure the Linux ".a" file. Let's explore the definition, purpose, structure, and methods of creating and executing the Linux ".a" file. What is L
