zoukankan      html  css  js  c++  java
  • Object-C类、方法、构造函数(2)

    Object-C 代码分为三部分:.h文件、.m文件及调用文件

    .h源文件

    #import <Foundation/Foundation.h>
    
    @interface Student:NSObject
    {
    NSString *studentName;
    NSInteger age;
    }
    -(void) printInfo;
    -(void) setStudentName:(NSString*) name;
    -(NSString*) studentName;
    -(NSInteger) age;
    
    @end
    

     .m源文件

    #import "Student.h"
    
    @implementation Student
    
    -(void) printInfo
    {
    NSLog(@"姓名:%@ 年龄:%d岁",studentName,studentAge);
    }
    -(void) setStudentName:(NSString*) name
    {
    studentName=name;
    }
    -(void)setAge:(NSInteger) age
    {
    studentAge=age;
    }
    -(NSString*) studentName
    {
    return studentName;
    }
    -(NSInteger) age
    {
    return studentAge;
    }
    @end
    

     调用源文件

    Student *student=[[Student alloc]init];  //init相当于实例化
    [student setStudentName:@"张三"];
    [student setAge:10];
    [student printInfo];
    [student release]; //资源释放
    

    实例方法和构造函数的总结

  • 相关阅读:
    hdu1089
    hdu1088
    驴妈妈
    途牛悄然上市 登陆SEC途牛前景如何
    途牛旅游网
    百度-----携程
    携程旅行网
    手机游戏
    web
    改造 改革
  • 原文地址:https://www.cnblogs.com/nidongde/p/5158017.html
Copyright © 2011-2022 走看看