zoukankan      html  css  js  c++  java
  • iOS方法重写

    在O-C中子类可以继承父类的方法 ,而不需要从新编写相同的方法,但是有有时候子类并不想原封不动的继承父类的方法,而且是想做一些修改,这就采用啦方法的重写,方法从写有叫做方法覆盖,若子类的中的方法与父类中的某一个方法具有相同的方法名,返回值类型和参数表,则新方法就会把原有的方法覆盖。

    父类:

    #import "MyViewController.h"


    @interface MyViewController ()


    @end


    @implementation MyViewController


    - (void)viewDidLoad

    {

        [superviewDidLoad];

        [selfsetX];

    // Do any additional setup after loading the view, typically from a nib.

    }


    - (void)didReceiveMemoryWarning

    {

        [superdidReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    -(void)setX

    {

       int a = 10;

        NSLog(@"A类方法的值:%zi",a);

        

    }

    @end


    子类:

    #import "MyBclassViewController.h"


    @interface MyBclassViewController ()


    @end


    @implementation MyBclassViewController


    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

    {

       self = [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];

       if (self) {

            // Custom initialization

        }

        return self;

    }


    - (void)viewDidLoad

    {

        [superviewDidLoad];

    }


    - (void)didReceiveMemoryWarning

    {

        [superdidReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    -(void)setX

    {

       int a = 100;

        NSLog(@"B类A的值:%zi",a);

        

    }

    方法 的重写 可以 写一些自己想要的控件 感觉挺实用的。

  • 相关阅读:
    973. K Closest Points to Origin
    919. Complete Binary Tree Inserter
    993. Cousins in Binary Tree
    20. Valid Parentheses
    141. Linked List Cycle
    912. Sort an Array
    各种排序方法总结
    509. Fibonacci Number
    374. Guess Number Higher or Lower
    238. Product of Array Except Self java solutions
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/7891230.html
Copyright © 2011-2022 走看看