Object-C learning path 1 (Hello World)

Source: Internet
Author: User

I started to learn oC from today, and I am very happy to write a hello world for my reference.

 

 
# Import <Foundation/Foundation. h> @ interface rectangle: nsobject {int width; int height;}-(void) setwidth :( INT) W setgeight :( INT) h;-(INT) geperimeter;-(INT) getarea; @ end

 

 
# Import "rectangle. H "@ implementation rectangle-(void) setwidth :( INT) W setgeight :( INT) h {width = W; Height = H;}-(INT) geperimeter {return (width + height) * 2;}-(INT) getarea {return width * height;} @ end

The square class inherits the rectangle class.

 

 

 
# Import "rectangle. H" @ interface square: rectangle-(void) setside :( INT) W;-(INT) side; @ end

 

 

 

# Import "Square. H "@ implementation square: rectangle-(void) setside :( INT) W {[self setwidth: W setgeight: W] ;}- (INT) side {return width ;} -(INT) getarea {return [self side] * [self side];} @ end

Main file:

 

 

//// Main. M // ocstart /// created by wildcat on 13-3-25. // copyright (c) 2013 wildcat. all rights reserved. // # import <Foundation/Foundation. h> # import "person. H "# import" rectangle. H "# import" Square. H "int main (INT argc, const char * argv []) {@ autoreleasepool {// insert code here... nslog (@ "Hello, world! "); Nslog (@" Hello, Lee !! "); // Define a person class person * P = [[person alloc] init]; [p setstr: @" Li xingle "]; [p myprint]; // define a rectangle Class Object rectangle * r = [[rectangle alloc] init]; [R setwidth: 3 setgeight: 4]; int area = [R getarea]; int perimeter = [R geperimeter]; nslog (@ "the area is: % d, the parimeter is % d", area, perimeter ); // define a square Instance Object square * s = [[Square alloc] init]; // enter the positive side length nslog on the keyboard (@ "Enter the side length of the square "); int number; scanf ("% I", & number); // set the side length [s setside: number]; int side = [s side]; int areas = [s getarea]; int perimeters = [s geperimeter]; nslog (@ "the square's side is: % d, the area is: % d, the primeter is: % d ", side, areas, perimeters); nsstring * Hello = @" sdfgdsf "; // convert it to uppercase Hello = [Hello uppercasestring]; nslog (Hello); // call the nslog (@ "the string length is: % lD", [Hello length]);} return 0 ;}

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.