程序猿的開(kāi)心一刻###
藺相如兼贸,司馬相如消返;魏無(wú)忌,長(zhǎng)孫無(wú)忌。下列哪一組對(duì)應(yīng)關(guān)系與此類似舶替?
a,PHP俩滥,Python躬它;b,JSP滓技,servlet哩牍;c,java令漂,java script 膝昆;d丸边,C,C++
請(qǐng)?jiān)谠u(píng)論區(qū)輸入答案荚孵,接下來(lái)進(jìn)入正題
Java接口#
參考文章:Java回調(diào)機(jī)制趣解
接口是什么:#####
- Java不支持多重繼承妹窖,但可以實(shí)現(xiàn)多個(gè)接口。
- 接口定義了某一批類所要遵守的規(guī)范收叶,只規(guī)定這些類里必須提供某些方法骄呼。
- 接口并不是類,編寫(xiě)接口的方式和類很相似滔驾,但是它們屬于不同的概念谒麦。
- 類描述對(duì)象的屬性和方法,接口則包含類要實(shí)現(xiàn)的方法哆致。
- 接口可以多繼承
接口的作用:#####
- 擴(kuò)展性: 如果業(yè)務(wù)邏輯發(fā)生變化需要新增類的方法绕德,就可以在不改變?cè)瓉?lái)已經(jīng)寫(xiě)好的代碼基礎(chǔ)上新增一個(gè)類來(lái)實(shí)現(xiàn)接口中定義的函數(shù)來(lái)實(shí)現(xiàn)。
- 規(guī)范性:告訴開(kāi)發(fā)人員你需要實(shí)現(xiàn)哪些業(yè)務(wù)摊阀,并可以將命名規(guī)范限制住耻蛇。
- 安全性:接口是實(shí)現(xiàn)軟件松耦合的重要手段,它描敘了系統(tǒng)對(duì)外的所有服務(wù)胞此,而不涉及任何具體的實(shí)現(xiàn)細(xì)節(jié)臣咖。
接口實(shí)例一:回調(diào)傳值
接口文件代碼:
public interface Listener {
void send(String s);
}
Info.java文件代碼
public class Info {
public Listener mListener;
public Info(Listener mListener){
this.mListener = mListener;
}
public void sends(){
mListener.send("haha");
}
}
要實(shí)現(xiàn)接口里的方法的MainActivity.java文件代碼
public class MainActivity extends AppCompatActivity implements Listener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Info info = new Info(this);
info.sends();
}
@Override
public void send(String s) {
System.out.println(s);
}
}
輸出結(jié)果####
1.png
思考:Java中接口和抽象類的區(qū)別?
Objective-C協(xié)議#
協(xié)議就是定義一套方法漱牵,遵守協(xié)議的類要去實(shí)現(xiàn)協(xié)議里的方法夺蛇。
iOS實(shí)例一 :控制器之間逆向傳值###
控制器一.m文件代碼:
#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()<MyProtocol>
@property(nonatomic,strong)UITextView* textView1;
@end
@implementation ViewController
//此方法只加載一次,類中成員對(duì)象和變量的初始化都會(huì)放在這個(gè)方法中
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blueColor];
_textView1 = [[UITextView alloc] initWithFrame:CGRectMake(0, 100, 320, 30)];
_textView1.font = [UIFont systemFontOfSize:15];
_textView1.textColor = [UIColor whiteColor];
_textView1.backgroundColor = [UIColor blackColor];
[self.view addSubview:_textView1];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(0, 50, 320, 30);
button1.titleLabel.font = [UIFont systemFontOfSize:17];
[button1 setTitle:@"點(diǎn)擊按鈕酣胀,去顯示控制器二" forState:UIControlStateNormal];
button1.backgroundColor = [UIColor blackColor];
[button1 addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
}
-(void)showMessage:(NSString *)str{
_textView1.text = str;
}
-(void)click{
SecondViewController *vc = [[SecondViewController alloc] init];
vc.delegate = self;
[self presentViewController:vc animated:YES completion:nil];
}
@end
控制器二.h文件代碼
#import <UIKit/UIKit.h>
//定義協(xié)議
@protocol MyProtocol <NSObject>
//必須實(shí)現(xiàn)
@required
-(void)showMessage:(NSString*)str;
//可選擇實(shí)現(xiàn)
@optional
-(void)showtext:(NSString*)str;
@end
@interface SecondViewController : UIViewController
@property(nonatomic,weak) id<MyProtocol> delegate;
@end
控制器二.m文件代碼
#import "SecondViewController.h"
@interface SecondViewController ()
@property(nonatomic,strong)UITextField *textField1;
@end
@implementation SecondViewController
//此方法只加載一次刁赦,類中成員對(duì)象和變量的初始化都會(huì)放在這個(gè)方法中
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
_textField1 = [[UITextField alloc] initWithFrame:CGRectMake(0, 100, 320, 30)];
_textField1.font = [UIFont systemFontOfSize:15];
_textField1.textColor = [UIColor whiteColor];
_textField1.backgroundColor = [UIColor blackColor];
[self.view addSubview:_textField1];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(0, 50, 320, 30);
button1.titleLabel.font = [UIFont systemFontOfSize:17];
[button1 setTitle:@"點(diǎn)擊按鈕,去顯示控制器一" forState:UIControlStateNormal];
button1.backgroundColor = [UIColor blackColor];
[button1 addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
}
-(void)click{
[_delegate showMessage:_textField1.text];
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
效果演示###
未命名.gif
iOS實(shí)例二 :View讓Controller幫它實(shí)現(xiàn)跳轉(zhuǎn)###
FirstView.h文件代碼
#import <UIKit/UIKit.h>
//定義協(xié)議
@protocol FirstViewDelegate <NSObject>
//必須實(shí)現(xiàn)
@required
-(void)jumpViewController;
//可選擇實(shí)現(xiàn)
@optional
-(void)showtext:(NSString*)str;
@end
@interface FirstView : UIView
@property(nonatomic,weak) id<FirstViewDelegate> delegate;
@end
FirstView.m文件代碼
#import "FirstView.h"
@implementation FirstView
-(id)initWithFrame:(CGRect)frame{
if(self = [super initWithFrame:frame]){
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(0, 50, 320, 30);
button1.titleLabel.font = [UIFont systemFontOfSize:17];
[button1 setTitle:@"點(diǎn)擊按鈕闻镶,去顯示控制器二" forState:UIControlStateNormal];
button1.backgroundColor = [UIColor blackColor];
[button1 addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button1];
}
return self;
}
-(void)click{
[_delegate jumpViewController];
}
@end
ViewController.m文件代碼
#import "ViewController.h"
#import "FirstView.h"
#import "SecondViewController.h"
@interface ViewController ()<FirstViewDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blueColor];
FirstView *firstView = [[FirstView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
firstView.delegate = self;
[self.view addSubview:firstView];
}
-(void)jumpViewController{
SecondViewController *vc = [[SecondViewController alloc] init];
[self presentViewController:vc animated:YES completion:nil];
}
@end
效果演示###
未命名.gif