Objective-C代码学习大纲(4)
本文为台湾出版的《Objective-C学习大纲》的翻译文档,系统介绍了Objective-C代码,很多名词为台湾同胞特指词汇,在学习时仔细研读才能体会。
继承、多型(Inheritance, Polymorphism)以及其他物件导向功能
id 型别
Objective-C 有种叫做 id 的型别,它的运作有时候像是 void*,不过它却严格规定只能用在物件。Objective-C 与 Java 跟 C++ 不一样,你在唿叫一个物件的 method 时,并不需要知道这个物件的型别。当然这个 method 一定要存在,这称为 Objective-C 的讯息传递。
- Fraction.h
- #import
- @interface Fraction: NSObject {
- int numerator;
- int denominator;
- }
- -(Fraction*) initWithNumerator: (int) n denominator: (int) d;
- -(void) print;
- -(void) setNumerator: (int) d;
- -(void) setDenominator: (int) d;
- -(void) setNumerator: (int) n andDenominator: (int) d;
- -(int) numerator;
- -(int) denominator;
- @end
- Fraction.m
- #import "Fraction.h"
- #import
- @implementation Fraction
- -(Fraction*) initWithNumerator: (int) n denominator: (int) d {
- self = [super init];
- if ( self ) {
- [self setNumerator: n andDenominator: d];
- }
- return self;
- }
- -(void) print {
- printf( "%i / %i", numerator, denominator );
- }
- -(void) setNumerator: (int) n {
- nnumerator = n;
- }
- -(void) setDenominator: (int) d {
- ddenominator = d;
- }
- -(void) setNumerator: (int) n andDenominator: (int) d {
- nnumerator = n;
- ddenominator = d;
- }
- -(int) denominator {
- return denominator;
- }
- -(int) numerator {
- return numerator;
- }
- @end
- Complex.h
- #import
- @interface Complex: NSObject {
- double real;
- double imaginary;
- }
- -(Complex*) initWithReal: (double) r andImaginary: (double) i;
- -(void) setReal: (double) r;
- -(void) setImaginary: (double) i;
- -(void) setReal: (double) r andImaginary: (double) i;
- -(double) real;
- -(double) imaginary;
- -(void) print;
- @end
- Complex.m
- #import "Complex.h"
- #import
- @implementation Complex
- -(Complex*) initWithReal: (double) r andImaginary: (double) i {
- self = [super init];
- if ( self ) {
- [self setReal: r andImaginary: i];
- }
- return self;
- }
- -(void) setReal: (double) r {
- rreal = r;
- }
- -(void) setImaginary: (double) i {
- iimaginary = i;
- }
- -(void) setReal: (double) r andImaginary: (double) i {
- rreal = r;
- iimaginary = i;
- }
- -(double) real {
- return real;
- }
- -(double) imaginary {
- return imaginary;
- }
- -(void) print {
- printf( "%_f + %_fi", real, imaginary );
- }
- @end
- main.m
- #import
- #import "Fraction.h"
- #import "Complex.h"
- int main( int argc, const char *argv[] ) {
- // create a new instance
- Fraction *frac = [[Fraction alloc] initWithNumerator: 1 denominator: 10];
- Complex *comp = [[Complex alloc] initWithReal: 10 andImaginary: 15];
- id number;
- // print fraction
- number = frac;
- printf( "The fraction is: " );
- [number print];
- printf( "\n" );
- // print complex
- number = comp;
- printf( "The complex number is: " );
- [number print];
- printf( "\n" );
- // free memory
- [frac release];
- [comp release];
- return 0;
- }
output
- The fraction is: 1 / 10
- The complex number is: 10.000000 + 15.000000i
这种动态连结有显而易见的好处。你不需要知道你唿叫 method 的那个东西是什么型别,如果这个物件对这个讯息有反应,那就会唤起这个 method。这也不会牵涉到一堆繁琐的转型动作,比如在 Java 裡唿叫一个整数物件的 .intValue() 就得先转型,然后才能唿叫这个 method。
继承(Inheritance)
- Rectangle.h
- #import
- @interface Rectangle: NSObject {
- int width;
- int height;
- }
- -(Rectangle*) initWithWidth: (int) w height: (int) h;
- -(void) setWidth: (int) w;
- -(void) setHeight: (int) h;
- -(void) setWidth: (int) w height: (int) h;
- -(int) width;
- -(int) height;
- -(void) print;
- @end
- Rectangle.m
- #import "Rectangle.h"
- #import
- @implementation Rectangle
- -(Rectangle*) initWithWidth: (int) w height: (int) h {
- self = [super init];
- if ( self ) {
- [self setWidth: w height: h];
- }
- return self;
- }
- -(void) setWidth: (int) w {
- wwidth = w;
- }
- -(void) setHeight: (int) h {
- hheight = h;
- }
- -(void) setWidth: (int) w height: (int) h {
- wwidth = w;
- hheight = h;
- }
- -(int) width {
- return width;
- }
- -(int) height {
- return height;
- }
- -(void) print {
- printf( "width = %i, height = %i", width, height );
- }
- @end
- Square.h
- #import "Rectangle.h"
- @interface Square: Rectangle
- -(Square*) initWithSize: (int) s;
- -(void) setSize: (int) s;
- -(int) size;
- @end
- Square.m
- #import "Square.h"
- @implementation Square
- -(Square*) initWithSize: (int) s {
- self = [super init];
- if ( self ) {
- [self setSize: s];
- }
- return self;
- }
- -(void) setSize: (int) s {
- width = s;
- height = s;
- }
- -(int) size {
- return width;
- }
- -(void) setWidth: (int) w {
- [self setSize: w];
- }
- -(void) setHeight: (int) h {
- [self setSize: h];
- }
- @end
- main.m
- #import "Square.h"
- #import "Rectangle.h"
- #import
- int main( int argc, const char *argv[] ) {
- Rectangle *rec = [[Rectangle alloc] initWithWidth: 10 height: 20];
- Square *sq = [[Square alloc] initWithSize: 15];
- // print em
- printf( "Rectangle: " );
- [rec print];
- printf( "\n" );
- printf( "Square: " );
- [sq print];
- printf( "\n" );
- // update square
- [sq setWidth: 20];
- printf( "Square after change: " );
- [sq print];
- printf( "\n" );
- // free memory
- [rec release];
- [sq release];
- return 0;
- }
output
- Rectangle: width = 10, height = 20
- Square: width = 15, height = 15
- Square after change: width = 20, height = 20
继承在 Objective-C 裡比较像 Java。当你扩充你的 super class(所以只能有一个 parent),你想自订这个 super class 的 method,只要简单的在你的 child class implementation 裡放上新的实作内容即可。而不需要 C++ 裡呆呆的 virtual table。
这裡还有一个值得玩味的地方,如果你企图像这样去唿叫 rectangle 的 constructor: Square *sq = [[Square alloc] initWithWidth: 10 height: 15],会发生什么事?答案是会产生一个编译器错误。因为 rectangle constructor 回传的型别是 Rectangle*,而不是 Square*,所以这行不通。在某种情况下如果你真想这样用,使用 id 型别会是很好的选择。如果你想使用 parent 的 constructor,只要把 Rectangle* 回传型别改成 id 即可。
动态识别(Dynamic types)
这裡有一些用于 Objective-C 动态识别的 methods(说明部分採中英并列,因为我觉得英文比较传神,中文怎么译都怪):
- -(BOOL) isKindOfClass: classObjis object a descendent or member of classObj
此物件是否是 classObj 的子孙或一员
- -(BOOL) isMemberOfClass: classObjis object a member of classObj
此物件是否是 classObj 的一员
- -(BOOL) respondsToSelector: selectordoes the object have a method named specifiec by the selector
此物件是否有叫做 selector 的 method
- +(BOOL) instancesRespondToSelector: selectordoes an object created by this class have the ability to respond to the specified selector
此物件是否是由有能力回应指定 selector 的物件所产生
- -(id) performSelector: selectorinvoke the specified selector on the object
唤起此物件的指定 selector
所有继承自 NSObject 都有一个可回传一个 class 物件的 class method。这非常近似于 Java 的 getClass() method。这个 class 物件被使用于前述的 methods 中。
Selectors 在 Objective-C 用以表示讯息。下一个範例会秀出建立 selector 的语法。
- main.m
- #import "Square.h"
- #import "Rectangle.h"
- #import
- int main( int argc, const char *argv[] ) {
- Rectangle *rec = [[Rectangle alloc] initWithWidth: 10 height: 20];
- Square *sq = [[Square alloc] initWithSize: 15];
- // isMemberOfClass
- // true
- if ( [sq isMemberOfClass: [Square class]] == YES ) {
- printf( "square is a member of square class\n" );
- }
- // false
- if ( [sq isMemberOfClass: [Rectangle class]] == YES ) {
- printf( "square is a member of rectangle class\n" );
- }
- // false
- if ( [sq isMemberOfClass: [NSObject class]] == YES ) {
- printf( "square is a member of object class\n" );
- }
- // isKindOfClass
- // true
- if ( [sq isKindOfClass: [Square class]] == YES ) {
- printf( "square is a kind of square class\n" );
- }
- // true
- if ( [sq isKindOfClass: [Rectangle class]] == YES ) {
- printf( "square is a kind of rectangle class\n" );
- }
- // true
- if ( [sq isKindOfClass: [NSObject class]] == YES ) {
- printf( "square is a kind of object class\n" );
- }
- // respondsToSelector
- // true
- if ( [sq respondsToSelector: @selector( setSize: )] == YES ) {
- printf( "square responds to setSize: method\n" );
- }
- // false
- if ( [sq respondsToSelector: @selector( nonExistant )] == YES ) {
- printf( "square responds to nonExistant method\n" );
- }
- // true
- if ( [Square respondsToSelector: @selector( alloc )] == YES ) {
- printf( "square class responds to alloc method\n" );
- }
- // instancesRespondToSelector
- // false
- if ( [Rectangle instancesRespondToSelector: @selector( setSize: )] == YES ) {
- printf( "rectangle instance responds to setSize: method\n" );
- }
- // true
- if ( [Square instancesRespondToSelector: @selector( setSize: )] == YES ) {
- printf( "square instance responds to setSize: method\n" );
- }
- // free memory
- [rec release];
- [sq release];
- return 0;
- }
output
- square is a member of square class
- square is a kind of square class
- square is a kind of rectangle class
- square is a kind of object class
- square responds to setSize: method
- square class responds to alloc method
- square instance responds to setSize: method
- Categories
当你想要为某个 class 新增 methods,你通常会扩充(extend,即继承)它。然而这不一定是个完美解法,特别是你想要重写一个 class 的某个功能,但你却没有塬始码时。Categories 允许你在现有的 class 加入新功能,但不需要扩充它。Ruby 语言也有类似的功能。
- FractionMath.h
- #import "Fraction.h"
- @interface Fraction (Math)
- -(Fraction*) add: (Fraction*) f;
- -(Fraction*) mul: (Fraction*) f;
- -(Fraction*) div: (Fraction*) f;
- -(Fraction*) sub: (Fraction*) f;
- @end
- FractionMath.m
- #import "FractionMath.h"
- @implementation Fraction (Math)
- -(Fraction*) add: (Fraction*) f {
- return [[Fraction alloc] initWithNumerator: numerator * [f denominator] +
- denominator * [f numerator]
- denominator: denominator * [f denominator]];
- }
- -(Fraction*) mul: (Fraction*) f {
- return [[Fraction alloc] initWithNumerator: numerator * [f numerator]
- denominator: denominator * [f denominator]];
- }
- -(Fraction*) div: (Fraction*) f {
- return [[Fraction alloc] initWithNumerator: numerator * [f denominator]
- denominator: denominator * [f numerator]];
- }
- -(Fraction*) sub: (Fraction*) f {
- return [[Fraction alloc] initWithNumerator: numerator * [f denominator] -
- denominator * [f numerator]
- denominator: denominator * [f denominator]];
- }
- @end
- main.m
- #import
- #import "Fraction.h"
- #import "FractionMath.h"
- int main( int argc, const char *argv[] ) {
- // create a new instance
- Fraction *frac1 = [[Fraction alloc] initWithNumerator: 1 denominator: 3];
- Fraction *frac2 = [[Fraction alloc] initWithNumerator: 2 denominator: 5];
- Fraction *frac3 = [frac1 mul: frac2];
- // print it
- [frac1 print];
- printf( " * " );
- [frac2 print];
- printf( " = " );
- [frac3 print];
- printf( "\n" );
- // free memory
- [frac1 release];
- [frac2 release];
- [frac3 release];
- return 0;
- }
output
- 1/3 * 2/5 = 2/15
重点是 @implementation 跟 @interface 这两行:@interface Fraction (Math) 以及 @implementation Fraction (Math).
(同一个 class)只能有一个同名的 category,其他的 categories 得加上不同的、独一无二的名字。
Categories 在建立 private methods 时十分有用。因为 Objective-C 并没有像 Java 这种 private/protected/public methods 的概念,所以必须要使用 categories 来达成这种功能。作法是把 private method 从你的 class header (.h) 档案移到 implementation (.m) 档案。以下是此种作法一个简短的範例。
- MyClass.h
- #import
- @interface MyClass: NSObject
- -(void) publicMethod;
- @end
- MyClass.m
- #import "MyClass.h"
- #import
- @implementation MyClass
- -(void) publicMethod {
- printf( "public method\n" );
- }
- @end
- // private methods
- @interface MyClass (Private)
- -(void) privateMethod;
- @end
- @implementation MyClass (Private)
- -(void) privateMethod {
- printf( "private method\n" );
- }
- @end
- main.m
- #import "MyClass.h"
- int main( int argc, const char *argv[] ) {
- MyClass *obj = [[MyClass alloc] init];
- // this compiles
- [obj publicMethod];
- // this throws errors when compiling
- //[obj privateMethod];
- // free memory
- [obj release];
- return 0;
- }
output
- public method
- Posing
Posing 有点像 categories,但是不太一样。它允许你扩充一个 class,并且全面性地的扮演(pose)这个 super class。例如:你有一个扩充 NSArray 的 NSArrayChild 物件。如果你让 NSArrayChild 扮演 NSArray,则在你的程式码中所有的 NSArray 都会自动被替代为 NSArrayChild。
- FractionB.h
- #import "Fraction.h"
- @interface FractionB: Fraction
- -(void) print;
- @end
- FractionB.m
- #import "FractionB.h"
- #import
- @implementation FractionB
- -(void) print {
- printf( "(%i/%i)", numerator, denominator );
- }
- @end
- main.m
- #import
- #import "Fraction.h"
- #import "FractionB.h"
- int main( int argc, const char *argv[] ) {
- Fraction *frac = [[Fraction alloc] initWithNumerator: 3 denominator: 10];
- // print it
- printf( "The fraction is: " );
- [frac print];
- printf( "\n" );
- // make FractionB pose as Fraction
- [FractionB poseAsClass: [Fraction class]];
- Fraction *frac2 = [[Fraction alloc] initWithNumerator: 3 denominator: 10];
- // print it
- printf( "The fraction is: " );
- [frac2 print];
- printf( "\n" );
- // free memory
- [frac release];
- [frac2 release];
- return 0;
- }
output
- The fraction is: 3/10
- The fraction is: (3/10)
这个程式的输出中,第一个 fraction 会输出 3/10,而第二个会输出 (3/10)。这是 FractionB 中实作的方式。
poseAsClass 这个 method 是 NSObject 的一部份,它允许 subclass 扮演 superclass。
Protocols
Objective-C 裡的 Protocol 与 Java 的 interface 或是 C++ 的 purely virtual class 相同。
- Printing.h
- @protocol Printing
- -(void) print;
- @end
- Fraction.h
- #import
- #import "Printing.h"
- @interface Fraction: NSObject {
- int numerator;
- int denominator;
- }
- -(Fraction*) initWithNumerator: (int) n denominator: (int) d;
- -(void) setNumerator: (int) d;
- -(void) setDenominator: (int) d;
- -(void) setNumerator: (int) n andDenominator: (int) d;
- -(int) numerator;
- -(int) denominator;
- @end
- Fraction.m
- #import "Fraction.h"
- #import
- @implementation Fraction
- -(Fraction*) initWithNumerator: (int) n denominator: (int) d {
- self = [super init];
- if ( self ) {
- [self setNumerator: n andDenominator: d];
- }
- return self;
- }
- -(void) print {
- printf( "%i/%i", numerator, denominator );
- }
- -(void) setNumerator: (int) n {
- nnumerator = n;
- }
- -(void) setDenominator: (int) d {
- ddenominator = d;
- }
- -(void) setNumerator: (int) n andDenominator: (int) d {
- nnumerator = n;
- ddenominator = d;
- }
- -(int) denominator {
- return denominator;
- }
- -(int) numerator {
- return numerator;
- }
- -(Fraction*) copyWithZone: (NSZone*) zone {
- return [[Fraction allocWithZone: zone] initWithNumerator: numerator
- denominator: denominator];
- }
- @end
- Complex.h
- #import
- #import "Printing.h"
- @interface Complex: NSObject {
- double real;
- double imaginary;
- }
- -(Complex*) initWithReal: (double) r andImaginary: (double) i;
- -(void) setReal: (double) r;
- -(void) setImaginary: (double) i;
- -(void) setReal: (double) r andImaginary: (double) i;
- -(double) real;
- -(double) imaginary;
- @end
- Complex.m
- #import "Complex.h"
- #import
- @implementation Complex
- -(Complex*) initWithReal: (double) r andImaginary: (double) i {
- self = [super init];
- if ( self ) {
- [self setReal: r andImaginary: i];
- }
- return self;
- }
- -(void) setReal: (double) r {
- rreal = r;
- }
- -(void) setImaginary: (double) i {
- iimaginary = i;
- }
- -(void) setReal: (double) r andImaginary: (double) i {
- rreal = r;
- iimaginary = i;
- }
- -(double) real {
- return real;
- }
- -(double) imaginary {
- return imaginary;
- }
- -(void) print {
- printf( "%_f + %_fi", real, imaginary );
- }
- @end
- main.m
- #import
- #import "Fraction.h"
- #import "Complex.h"
- int main( int argc, const char *argv[] ) {
- // create a new instance
- Fraction *frac = [[Fraction alloc] initWithNumerator: 3 denominator: 10];
- Complex *comp = [[Complex alloc] initWithReal: 5 andImaginary: 15];
- id printable;
- id copyPrintable;
- // print it
- printable = frac;
- printf( "The fraction is: " );
- [printable print];
- printf( "\n" );
- // print complex
- printable = comp;
- printf( "The complex number is: " );
- [printable print];
- printf( "\n" );
- // this compiles because Fraction comforms to both Printing and NSCopyable
- copyPrintable = frac;
- // this doesn't compile because Complex only conforms to Printing
- //copyPrintable = comp;
- // test conformance
- // true
- if ( [frac conformsToProtocol: @protocol( NSCopying )] == YES ) {
- printf( "Fraction conforms to NSCopying\n" );
- }
- // false
- if ( [comp conformsToProtocol: @protocol( NSCopying )] == YES ) {
- printf( "Complex conforms to NSCopying\n" );
- }
- // free memory
- [frac release];
- [comp release];
- return 0;
- }
output
- The fraction is: 3/10
- The complex number is: 5.000000 + 15.000000i
- Fraction conforms to NSCopying
protocol 的宣告十分简单,基本上就是 @protocol ProtocolName (methods you must implement) @end。
要遵从(conform)某个 protocol,将要遵从的 protocols 放在 <> 裡面,并以逗点分隔。如:@interface SomeClass
protocol 要求实作的 methods 不需要放在 header 档裡面的 methods 列表中。如你所见,Complex.h 档案裡没有 -(void) print 的宣告,却还是要实作它,因为它(Complex class)遵从了这个 protocol。
Objective-C 的介面系统有一个独一无二的观念是如何指定一个型别。比起 C++ 或 Java 的指定方式,如:Printing *someVar = ( Printing * ) frac; 你可以使用 id 型别加上 protocol:id var = frac;。这让你可以动态地指定一个要求多个 protocol 的型别,却从头到尾只用了一个变数。如: var = frac;
就像使用@selector 来测试物件的继承关係,你可以使用 @protocol 来测试物件是否遵从介面。如果物件遵从这个介面,[object conformsToProtocol: @protocol( SomeProtocol )] 会回传一个 YES 型态的 BOOL 物件。同样地,对 class 而言也能如法炮製 [SomeClass conformsToProtocol: @protocol( SomeProtocol )]。
Objective-C代码学习大纲(4)的更多相关文章
- Objective-C代码学习大纲(3)
Objective-C代码学习大纲(3) 2011-05-11 14:06 佚名 otierney 字号:T | T 本文为台湾出版的<Objective-C学习大纲>的翻译文档,系统介绍 ...
- Objective-C代码学习大纲(6)
2011-05-11 14:06 佚名 otierney 字号:T | T 本文为台湾出版的<Objective-C学习大纲>的翻译文档,系统介绍了Objective-C代码,很多名词为台 ...
- Objective-C代码学习大纲(5)
2011-05-11 14:06 佚名 otierney 字号:T | T 本文为台湾出版的<Objective-C学习大纲>的翻译文档,系统介绍了Objective-C代码,很多名词为台 ...
- Objective-C代码学习大纲(2)
2011-05-11 14:06 佚名 otierney 字号:T | T 本文为台湾出版的<Objective-C学习大纲>的翻译文档,系统介绍了Objective-C代码,很多名词为台 ...
- Objective-C代码学习大纲(1)
2011-05-11 14:06 佚名 otierney 字号:T | T 本文为台湾出版的<Objective-C学习大纲>的翻译文档,系统介绍了Objective-C代码,很多名词为台 ...
- 大数据Python学习大纲
最近公司在写一个课程<大数据运维实训课>,分为4个部分,linux实训课.Python开发.hadoop基础知识和项目实战.这门课程主要针对刚从学校毕业的学生去应聘时不会像一个小白菜一样被 ...
- JVM学习——学习方法论&学习大纲
2020年02月06日22:25:51 完成了Springboot系列的学习和Kafka的学习,接下来进入JVM的学习阶段 深入理解JVM 学习方法论 如何去学习一门课程--方法论 多讨论,从别人身上 ...
- u-boot代码学习内容
前言 u-boot代码庞大,不可能全部细读,只能有选择的读部分代码.在读代码之前,根据韦东山教材,关于代码学习内容和深度做以下预先划定. 一.Makefile.mkconfig.config.mk等 ...
- Linux 系统从入门到精通的学习大纲;
以前没有接触过Linux,生产环境需要,有时候遇到问题,百度一下,问题解决了,在遇到问题,在百度,有时候问题是如何解决的,为什么会解决有点丈二的和尚摸不着头脑, 为此,想用一段时间,系统的学习下Lin ...
随机推荐
- MFC的CListCtrl双击获取单元格的行列
首先要把ClistCtrl设置为整选中模式和网格模式 SetExtendedStyle(LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT); 重写方法如下: void CAl ...
- ocr 识别 github 源码
参考 [1] https://github.com/eragonruan/text-detection-ctpn [2] https://github.com/senlinuc/caffe_ocr [ ...
- Python sql注入 过滤字符串的非法字符
#coding:utf8 #在开发过程中,要对前端传过来的数据进行验证,防止sql注入攻击,其中的一个方案就是过滤用户传过来的非法的字符 def sql_filter(sql, max_length= ...
- Linux find 命令概览
Linux下find命令在目录结构中搜索文件,并执行指定的操作.Linux下find命令提供了相当多的查找条件,功能很强大.由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时 ...
- StrongLoop
http://loopback.io/getting-started/ 使用 StrongLoop 创建 Node.js MySQL 应用程序 StrongLoop 是 IBM 的一家子公司,Stro ...
- atitit.研发管理--标准化流程总结---java开发环境与项目部署环境的搭建工具包总结
atitit.研发管理--标准化流程总结---java开发环境与项目部署环境的搭建工具包总结 1. ide系列(只开发环境需要,但部署环境也做好放上,很有用) 1 2. web服务器+sdk+网站程序 ...
- tcpdump常用参数说明
(一).学习tcpdump的5个参数 初次使用tcpdump时,使用tcpdump -h命令可以看到它有数十个参数. 根据我们在运维工作中的经验,掌握tcpdump以下5个参数即可满足大部分的工作需要 ...
- shell判断文件夹是否存在
#shell判断文件夹是否存在 #如果文件夹不存在,创建文件夹 if [ ! -d "/myfolder" ]; then mkdir /myfolder fi #shell判断文 ...
- iOS字符串安全
iOS字符串安全 一个编译成功的可执行程序,其中已初始化的字符串都是完整可见的. 针对于iOS的Mach-O二进制通常可获得以下几种字符串信息: 资源文件名 可见的函数符号名 SQL语句 format ...
- js基本知识2
一.提示框 1. 弹出警示框 alert(); window.alert(); window 窗口 2. 控制台输出 console.log() 3. 文档打印 document 文档 documen ...