• oc33--构造方法2


    //  Person.h
    
    #import <Foundation/Foundation.h>
    
    @interface Person : NSObject
    @property int age;  //属性是_age
    @end
    //
    //  Person.m
    
    #import "Person.h"
    
    @implementation Person
    
    - (instancetype)init
    {
        if (self  = [super init]) {
            _age = 10;
            [self setAge:100];
        }
        return self;
    }
    
    - (NSString *)description
    {
        return [NSString stringWithFormat:@"age = %i", _age];
    }
    @end
    //
    //  Studnet.h
    
    #import "Person.h"
    
    @interface Studnet : Person
    
    @property int no; // 学号,  属性是_no,
    
    @end
    //
    //  Studnet.m
    
    #import "Studnet.h"
    
    @implementation Studnet
    
    - (instancetype)init
    {
        if (self = [super init]) {
           // [self setAge:11];
            _no = 1;
            [self setNo:22];
        }
        return self;
    }
    
    - (NSString *)description
    {
        return [NSString stringWithFormat:@"age = %i , no = %i,%i", [self age], _no,[self no]];
    }
    @end
    //
    //  main.m
    //  构造方法练习
    
    #import <Foundation/Foundation.h>
    #import "Person.h"
    #import "Studnet.h"
    
    int main(int argc, const char * argv[]) {
        Person *p = [[Person alloc] init];
        NSLog(@"%@", p);
        
        Person *p1 = [[Person alloc] init];
        NSLog(@"%@", p1);
        
        Studnet *stu = [[Studnet alloc] init];
        NSLog(@"%@", stu);
        
        Studnet *stu1 = [[Studnet alloc] init];
        NSLog(@"%@", stu1);
        
        return 0;
    }
  • 相关阅读:
    windows常规
    oracle常规操作
    idea使用
    java-maven
    java-rabbimq
    centos7 rabbitMq 安装教程
    Leetcode 332.重新安排行程
    Leetcode 334.递增的三元子序列
    Leetcode 331.验证二叉树的前序序列化
    Leetcode 330.按要求补齐数组
  • 原文地址:https://www.cnblogs.com/yaowen/p/7416907.html
Copyright © 2020-2023  润新知