Obj-c3[继承,构造便利器]

main.m

#import <Foundation/Foundation.h>

#import "Person.h"

#import "Student.h"

#import "COllegeStudent.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
//继承
        
//        Person * person = [[Person alloc] init];
//        [person sayHi];
//        Student * student = [[Student alloc] init];
//        [student sayHi];
//        
//        COllegeStudent * p = [[COllegeStudent alloc]init];
//        [p sayHi];
//        
//        Student * student1 = [[Student alloc] initWithName:@"张三" sex:@"男" age:19 number:10005 score:89];
//        [student1 sayHi];
//        COllegeStudent * p = [[COllegeStudent alloc] initWithName:@"战神嘎哈" sex:@"男" age:55 number:101111 score:99.99 major:@"计算机" academy:@"计算机世界"];
//        [p sayHi];
//        COllegeStudent * p1 = [[COllegeStudent alloc] initWithName:@"Tom" sex:@"M" age:19 number:1005];
//        [p1 sayHi];
        
        Person * p1 = [Person personWithName:@"张三"];
        [p1 sayHi];
        
        
//        Student * student1 = [[Student alloc] init];
//        [student1 setName:@"张三"];
//        [student1 setSex:@"男"];
//        [student1 setAge:25];
//        [student1 setNumber:10001];
//        [student1 setScore:88.5];
//        [student1 sayHi];
//        [student1 study];
        
//        COllegeStudent * collageStudent1 = [[COllegeStudent alloc] init];
//        [collageStudent1 setName:@"杨紫"];
//        [collageStudent1 setSex:@"女"];
//        [collageStudent1 setAge:23];
//        [collageStudent1 setNumber:100012];
//        [collageStudent1 setScore:99.9];
//        [collageStudent1 setMajor:@"表演"];
//        [collageStudent1 setAcademy:@"北影"];
//        [collageStudent1 sayHi];
        
        
//        Dog * dog = [[Dog alloc] init];
//        [dog call];
//        
//        [Dog testMnethod];
//        
//        Cat * cat = [[Cat alloc] init];
//            NSLog(@"%@",cat);
//        [cat call];
//        
//        Cat * cat1 =[[Cat alloc] init];
//            NSLog(@"%@",cat1);
//        [cat1 call];
//            
    }
    return 0;
}
------------Person.h

#import <Foundation/Foundation.h>

@interface Person : NSObject
{
    NSString * _name;
    NSString * _sex;
    NSInteger  _age;
}

- (void)sayHi;

- (id)init;
//便利构造器
//+ 号方法,返回值是id,方法名一类名打头
//+ (id)personWithName:(NSString *)name;

- (id)initWithName:(NSString *)name;
+ (id)personWithName:(NSString *)name;

- (id)initWithSex:(NSString *)sex;
+ (id)personWithSex:(NSString *)sex;

- (id)initWithName:(NSString *)name
               sex:(NSString *)sex;
+ (id)personWithName:(NSString *)name
                 sex:(NSString *)sex;

- (id)initWithName:(NSString *)name
               sex:(NSString *)sex
               age:(NSInteger)age;
+ (id)personWithName:(NSString *)name
                 sex:(NSString *)sex
                 age:(NSInteger)age;

- (void)setName:(NSString *)name;
- (NSString *)name;

- (void)setSex:(NSString *)sex;
- (NSString *)sex;

- (void)setAge:(NSInteger)age;
- (NSInteger)age;

@end

----------Person.m

#import "Person.h"

@implementation Person

- (void)sayHi{
    NSLog(@"姓名:%@,年龄:%ld,性别%@",_name, _age, _sex);
}
- (id)init{
    self = [super init];
    if (self) {
        _name = @"张志义";
        _age = 18;
        _sex = @"男";
    }
//    _name = @"张志义";
//    _age = 18;
//    _sex = @"男";

return self;

}

- (id)initWithName:(NSString *)name{
//    self = [super init];
//    if (self) {
//        _name = name;
//    }
//    return self;

return [self initWithName:name sex:nil age:0];
}

//便利构造器
//+ 号方法,返回值是id,方法名一类名打头
+ (id)personWithName:(NSString *)name
{
    Person * p = [[Person alloc] initWithName:name];
    return p;
}

- (id)initWithSex:(NSString *)sex{
//    self = [super init];
//    if (self) {
//        _sex = sex;
//    }
//    return self;
    return [self initWithName:nil sex:sex age:0];
}
+ (id)personWithSex:(NSString *)sex{
    Person * p = [[Person alloc] initWithSex:sex];
    return p;
}

- (id)initWithName:(NSString *)name
               sex:(NSString *)sex{
//    self = [super init];
//    if (self) {
//        _name = name;
//        _sex = sex;
//    }
//    return self;
    return [self initWithName:name sex:sex age:0];
}
+ (id)personWithName:(NSString *)name
                 sex:(NSString *)sex{
    Person * p = [[Person alloc] initWithName:name sex:sex];
    return p;
}

//指定初始化方法,无论调用哪个初始化方法,最终调用到的初始化方法,称为是指定初始化方法,一个类只有一个指定初始化方法
- (id)initWithName:(NSString *)name
               sex:(NSString *)sex
               age:(NSInteger)age{
    self = [super init];
    if (self) {
        _name = name;
        _sex = sex;
        _age = age;
    }
    return self;
}
+ (id)personWithName:(NSString *)name
                 sex:(NSString *)sex
                 age:(NSInteger)age{
    Person * p = [[Person alloc] initWithName:name sex:sex age:age];
    return p;
}

- (void)setName:(NSString *)name{
    _name = name;
}
- (NSString *)name{
    return _name;
}

- (void)setSex:(NSString *)sex{
    _sex = sex;
}
- (NSString *)sex{
    return _sex;
}

- (void)setAge:(NSInteger)age{
    _age = age;}
- (NSInteger)age{
    return _age;
}

@end

---------------------Student.h

#import "Person.h"

@interface Student : Person
{
    NSInteger _number;
    CGFloat   _score;
}

- (id)initWithName:(NSString *)name
               sex:(NSString *)sex
               age:(NSInteger)age
            number:(NSInteger)number;
+ (id)studentWithName:(NSString *)name
                  sex:(NSString *)sex
                  age:(NSInteger)age
               number:(NSInteger)number;

- (id)initWithName:(NSString *)name
               sex:(NSString *)sex
               age:(NSInteger)age
            number:(NSInteger)number
             score:(CGFloat)score;

+ (id)studentWithName:(NSString *)name
                  sex:(NSString *)sex
                  age:(NSInteger)age
               number:(NSInteger)number
                score:(CGFloat)score;

- (void)setNumber:(NSInteger)number;
- (NSInteger)number;

- (void)setScore:(CGFloat)score;
- (CGFloat)score;

- (id)init;

- (void)study;

@end

----------------Student.m

//重写父类方法,调用的时候就会执行子类实现,如果子类没有重写,会逐级向上查找,找到即执行
- (void)sayHi{
    NSLog(@"I am a student, name is %@, sex is %@, age is %ld,number is %ld, score is %.2f",_name, _sex, _age, _number, _score);
}

- (id)init{
    
        self = [super init];
        if (self) {
                _number = 100002;
                _score = 86.6;
        }
//    _name = @"王武";
//    _age = 19;
//    _sex = @"男";
//    _number = 100002;
//    _score = 86.6;
    return self;
}

- (id)initWithName:(NSString *)name
               sex:(NSString *)sex
               age:(NSInteger)age
            number:(NSInteger)number{
    return [self initWithName:name sex:sex age:age number:number score:0];
}

+ (id)studentWithName:(NSString *)name
                  sex:(NSString *)sex
                  age:(NSInteger)age
               number:(NSInteger)number{
    
    Student * p = [[Student alloc] initWithName:name sex:sex age:age number:number];
    return  p;
    
}

- (id)initWithName:(NSString *)name
               sex:(NSString *)sex
               age:(NSInteger)age
            number:(NSInteger)number
             score:(CGFloat)score{
//    self = [super self];
//    if (self) {
//        _name = name;
//        _sex = sex;
//        _age = age;
//        _number = number;
//        _score = score;
//    }
//    return self;
    self = [super initWithName:name sex:sex age:age];
    if (self) {
        _number = number;
        _score = score;
    }
    return self;
}

+ (id)studentWithName:(NSString *)name
                  sex:(NSString *)sex
                  age:(NSInteger)age
               number:(NSInteger)number
                score:(CGFloat)score{

Student * p = [[Student alloc] initWithName:name sex:sex age:age number:number score:score];
    return  p;
    
}
- (void)study{
    NSLog(@"学习");
}

- (void)setNumber:(NSInteger)number{
    _number = number;
}
- (NSInteger)number{
    return _number;
}

- (void)setScore:(CGFloat)score{
    _score = score;
}
- (CGFloat)score{
    return _score;
}

@end

------------COllegeStudent.h

#import "Student.h"

@interface COllegeStudent : Student
{
    NSString * _major;//专业
    NSString * _academy;//学院
}
- (id)initWithName:(NSString *)name
               sex:(NSString *)sex;

- (id)initWithName:(NSString *)name
               sex:(NSString *)sex
               age:(NSInteger)age
            number:(NSInteger)number;
- (id)initWithName:(NSString *)name
               sex:(NSString *)sex
               age:(NSInteger)age
            number:(NSInteger)number
             score:(CGFloat)score
             major:(NSString *)major;
- (id)initWithName:(NSString *)name
               sex:(NSString *)sex
               age:(NSInteger)age
            number:(NSInteger)number
             score:(CGFloat)score
             major:(NSString *)major
           academy:(NSString *)academy;

- (id)init;
- (void)setMajor:(NSString *)major;
- (NSString *)major;

- (void)setAcademy:(NSString *)academy;
- (NSString *)academy;
@end

------------COllegeStudent.m

#import "COllegeStudent.h"

@implementation COllegeStudent

//重写父类方法,调用的时候就会执行子类实现,如果子类没有重写,会逐级向上查找,找到即执行
- (void)sayHi{
    NSLog(@"I am a student, name is %@, sex is %@, age is %ld,number is %ld, score is %.2f, major is %@, academy is %@",_name, _sex, _age, _number, _score, _major, _academy);
}

- (id)init{
    
    
    self = [super init];
    if (self) {
        _major = @"软件开发";
        _academy = @"软件工程";
    }
//    _name = @"王武";
//    _age = 19;
//    _sex = @"男";
//    _number = 100002;
//    _score = 86.6;
//    _major = @"软件开发";
//    _academy = @"软件工程";
    return self;
}
- (id)initWithName:(NSString *)name
               sex:(NSString *)sex{
    return [self initWithName:name sex:sex age:0 number:0 score:0 major:nil academy:nil];
}
- (id)initWithName:(NSString *)name
               sex:(NSString *)sex
               age:(NSInteger)age
            number:(NSInteger)number{
    return [self initWithName:name sex:sex age:age number:number score:0 major:nil academy:nil];
}
- (id)initWithName:(NSString *)name
               sex:(NSString *)sex
               age:(NSInteger)age
            number:(NSInteger)number
             score:(CGFloat)score
             major:(NSString *)major{
    return [self initWithName:name sex:sex age:age number:number score:score major:major academy:nil];
}
- (id)initWithName:(NSString *)name
               sex:(NSString *)sex
               age:(NSInteger)age
            number:(NSInteger)number
             score:(CGFloat)score
             major:(NSString *)major
           academy:(NSString *)academy{
    self = [super initWithName:name sex:sex age:age number:number score:score];
    if (self) {
        _major = major;
        _academy = academy;
    }
    return self;
}

- (void)setMajor:(NSString *)major{
    _major = major;
}
- (NSString *)major{
    return _major;
}

- (void)setAcademy:(NSString *)academy{
    _academy = academy;
}
- (NSString *)academy{
    return _academy;
}
@end

时间: 2024-12-28 22:03:09

Obj-c3[继承,构造便利器]的相关文章

c++11 继承构造

c++11 继承构造 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #include <vector> #include <map> // C++ 11允许派生类继承基类的构造函数(默认构造函数.复制构造函数.移动构造函数除外). /* 注意: 继承的构造函数只能初始化基类中的成员变量,不能初始化派生类的成员变量 如果基类的构造函数被声明为私有,或者派生类是从基类中

c++继承构造析构调用原则以及特殊变量处理

一.继承中的构造析构调用原则 1.子类对象在创建时会首先调用父类的构造函数 2.父类构造函数执行结束后,执行子类构造函数 3.当父类构造函数有参数时,需要在子类的初始化列表中显示调用: 4.析构函数调用的先后顺序与构造函数相反 二.继承与组合混搭时,构造与析构的调用原则 1.先析构父类,再构造成员变量,最后构造自己 2.先析构自己,在析构成员变量,最后析构父类(先析构的对象,后释放) 三.继承中同名成员变量处理方法 1.当子类成员变量与父类的成员变量同名时,子类依然从父类继承同名成员,且同名成员

scala 第12讲 继承 构造

DT大数据梦工厂scala的所有视频.PPT和代码在百度云盘的链接:http://pan.baidu.com/share/home?uk=4013289088#category/type=0&qq-pf-to=pcqq.group <Scala深入浅出实战初级入门经典视频课程>http://edu.51cto.com/lesson/id-66538.html <Scala深入浅出实战中级进阶经典视频课程>http://edu.51cto.com/lesson/id-6713

ios不可改变NSString类和可改变NSMutableString类的使用

一, NSString的常用用法 1,创建对象两种方法 1)   "-"号方法  alloc + 初始化initWithString  NSString *str = [[NSString alloc] initWithString:@"iPhone 4"];  initWithFormat  这种方法比initWithString更强大可以设置格式,如上述的字符串" iPhone 4" 可以写成NSString *str = [[NSStrin

继承 多态 构造 易错点

说明 继承的二个关键点: 子类继承了其父类中所有非私有的成员变量和成员方法,并作为自己的成员变量和方法 子类中定义的成员变量和父类中定义的成员变量相同时,则父类中的成员变量会被[隐藏] 子父类成员同名情况 对于成员变量:父类的成员变量在子类中是存在的,即使是私有的或者和子类中定义的成员变量同名:但是子类中能看到的只有子类中定义的变量和从父类继承的非私有的变量,并且如果子类中定义的变量和从父类中继承过来的变量重名,则会隐藏从父类继承的成员变量. 对于成员方法:同名的非静态方法则是我们常说的实实在在

js继承关系

跟传统面向对象语言比起来,js在继承关系方面比较特别,如果第一次看恐怕会有些抓狂,偶就是这样(又透露小白本质#=_=),从哪里说起好呢?函数调用? js中函数的调用方式大致可分以下几种: 1. 普通函数,直接调用 function Hi(){ alert(233); } Hi(); var f = function(){ alert(666); }; f(); 2. 作为对象的方法调用 var obj = { x:1, m:function(){ alert("hello"); } }

python-类的继承

1.简单继承 class F1: #父类,或者基类 def show(self): print('F1.show') class F2(F1): #表示F2继承F1.F2叫子类,或者派生类 def bar(self): print('bar') obj = F2() obj.show() obj.bar() out: F1.showbar 2.父类和子类有相同的方法,优先执行子类中的方法 class F1: #父类,或者基类 def show(self): print('F1.show') cl

JavaScript学习笔记(三)—— 继承的实现

一.JavaScript实现继承的三层含义:   ① 子类的实例可以共享父类的方法: ② 子类可以覆盖父类的方法或者扩展新的方法: ③ 子类和父类都是子类的实例. 二.实现继承的几种方法: 1.原型链继承 例:function Box( ){           //被继承的函数叫超(父)类型: this.name = “Andy”; } function Desk( ){           //继承的函数叫子类型 this.age = 25; } Desk.prototype = new B

lua实现多继承

lua对于面向对象的支持主要通过table来实现,每个table都是一个对象,对于继承,lua有元表的机制,通过setmetatble()可以来修改元表, 元表是什么? 元表其实就是在本身找不到的东西,就会去元表中查找. __index元方法: __index方法是用来确定一个表在被作为元表时的查找方法.它的值可以使函数也可是是table,一般用法为: local obj = {} setmetatable(obj, {__index = xx}) --这样便实现了obj实现继承自xx. 对于多