diff options
-rw-r--r-- | common-lisp.html.markdown | 5 | ||||
-rw-r--r-- | csharp.html.markdown | 973 | ||||
-rw-r--r-- | ko-kr/lua-kr.html.markdown | 5 | ||||
-rw-r--r-- | objective-c.html.markdown | 310 | ||||
-rw-r--r-- | zh-cn/racket-cn.html.markdown | 1 |
5 files changed, 804 insertions, 490 deletions
diff --git a/common-lisp.html.markdown b/common-lisp.html.markdown index 24b6947f..a917304c 100644 --- a/common-lisp.html.markdown +++ b/common-lisp.html.markdown @@ -1,6 +1,6 @@ --- -language: commonlisp +language: "Common Lisp" filename: commonlisp.lisp contributors: - ["Paul Nathan", "https://github.com/pnathan"] @@ -17,7 +17,7 @@ Another popular and recent book is -```commonlisp +```scheme ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; 0. Syntax @@ -589,6 +589,7 @@ nil ; for false - and the empty list ;; regular code. ;; See Practical Common Lisp for more information on macros. +``` ## Further Reading diff --git a/csharp.html.markdown b/csharp.html.markdown index e079571e..c254b5a9 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -28,492 +28,493 @@ using System.Collections.Generic; // defines scope to organize code into "packages" namespace Learning { - // Each .cs file should at least contain a class with the same name as the file - // you're allowed to do otherwise, but shouldn't for sanity. - public class LearnCSharp - { - // A console application must have a main method as an entry point - public static void Main(string[] args) - { - // Use Console.WriteLine to print lines - Console.WriteLine("Hello World"); - Console.WriteLine( - "Integer: " + 10 + - " Double: " + 3.14 + - " Boolean: " + true); - - // To print without a new line, use Console.Write - Console.Write("Hello "); - Console.Write("World"); - - - /////////////////////////////////////////////////// - // Types & Variables - // - // Declare a variable using <type> <name> - /////////////////////////////////////////////////// - - // Sbyte - Signed 8-bit integer - // (-128 <= sbyte <= 127) - sbyte fooSbyte = 100; - - // Byte - Unsigned 8-bit integer - // (0 <= byte <= 255) - byte fooByte = 100; - - // Short - Signed 16-bit integer - // (-32,768 <= short <= 32,767) - short fooShort = 10000; - - // Ushort - Unsigned 16-bit integer - // (0 <= ushort <= 65,535) - ushort fooUshort = 10000; - - // Integer - Signed 32-bit integer - // (-2,147,483,648 <= int <= 2,147,483,647) - int fooInt = 1; - - // Uinteger - Unsigned 32-bit integer - // (0 <= uint <= 4,294,967,295) - uint fooUint = 1; - - // Long - Signed 64-bit integer - // (-9,223,372,036,854,775,808 <= long <= 9,223,372,036,854,775,807) - long fooLong = 100000L; - // L is used to denote that this variable value is of type long or ulong - // anything without is treated as int or uint depending on size. - - // Ulong - Unsigned 64-bit integer - // (0 <= ulong <= 18,446,744,073,709,551,615) - ulong fooUlong = 100000L; - - // Float - Single-precision 32-bit IEEE 754 Floating Point - // Precision: 7 digits - float fooFloat = 234.5f; - // f is used to denote that this variable value is of type float; - // otherwise it is treated as double. - - // Double - Double-precision 64-bit IEEE 754 Floating Point - // Precision: 15-16 digits - double fooDouble = 123.4; - - // Bool - true & false - bool fooBoolean = true; - bool barBoolean = false; - - // Char - A single 16-bit Unicode character - char fooChar = 'A'; - - // Strings - string fooString = "My string is here!"; - Console.WriteLine(fooString); - - // formatting - string fooFormattedString = string.Format("Check Check, {0} {1}, {0} {1:0.0}", 1, 2); - Console.WriteLine(fooFormattedString); - - // formatting dates - DateTime fooDate = DateTime.Now; - Console.WriteLine(fooDate.ToString("hh:mm, dd MMM yyyy")); - - // \n is an escaped character that starts a new line - string barString = "Printing on a new line?\nNo Problem!"; - Console.WriteLine(barString); - - // it can be written prettier by using the @ symbol - string bazString = @"Here's some stuff - on a new line!"; - Console.WriteLine(bazString); - - // quotes need to be escaped - // use \" normally - string quotedString = "some \"quoted\" stuff"; - Console.WriteLine(quotedString); - - // use "" when strings start with @ - string quotedString2 = @"some MORE ""quoted"" stuff"; - Console.WriteLine(quotedString2); - - // Use const or read-only to make a variable immutable - // const values are calculated at compile time - const int HOURS_I_WORK_PER_WEEK = 9001; - - // Nullable types - // any type can be made nullable by suffixing a ? - // <type>? <var name> = <value> - int? nullable = null; - Console.WriteLine("Nullable variable: " + nullable); - - // ?? is syntactic sugar for specifying default value - // in case variable is null - int notNullable = nullable ?? 0; - Console.WriteLine("Not nullable variable: " + notNullable); - - // Var - compiler will choose the most appropriate type based on value - var fooImplicit = true; - - /////////////////////////////////////////////////// - // Data Structures - /////////////////////////////////////////////////// - Console.WriteLine("\n->Data Structures"); - - // Arrays - // The array size must be decided upon declaration - // The format for declaring an array is follows: - // <datatype>[] <var name> = new <datatype>[<array size>]; - int[] intArray = new int[10]; - string[] stringArray = new string[1]; - bool[] boolArray = new bool[100]; - - // Another way to declare & initialize an array - int[] y = { 9000, 1000, 1337 }; - - // Indexing an array - Accessing an element - Console.WriteLine("intArray @ 0: " + intArray[0]); - - // Arrays are zero-indexed and mutable. - intArray[1] = 1; - Console.WriteLine("intArray @ 1: " + intArray[1]); // => 1 - - // Lists - // Lists are used more frequently than arrays as they are more flexible - // The format for declaring a list is follows: - // List<datatype> <var name> = new List<datatype>(); - List<int> intList = new List<int>(); - List<string> stringList = new List<string>(); - - // Another way to declare & initialize a list - List<int> z = new List<int> { 9000, 1000, 1337 }; - - // Indexing a list - Accessing an element - // Lists are zero-indexed and mutable. - Console.WriteLine("z @ 0: " + z[2]); - - // Lists don't default to a value; - // A value must be added before accessing the index - intList.Add(1); - Console.WriteLine("intList @ 0: " + intList[0]); - - - // Others data structures to check out: - // - // Stack/Queue - // Dictionary - // Read-only Collections - // Tuple (.Net 4+) - - - /////////////////////////////////////// - // Operators - /////////////////////////////////////// - Console.WriteLine("\n->Operators"); - - int i1 = 1, i2 = 2; // Shorthand for multiple declarations - - // Arithmetic is straightforward - Console.WriteLine("1+2 = " + (i1 + i2)); // => 3 - Console.WriteLine("2-1 = " + (i2 - i1)); // => 1 - Console.WriteLine("2*1 = " + (i2 * i1)); // => 2 - Console.WriteLine("1/2 = " + (i1 / i2)); // => 0 (0.5 truncated down) - - // Modulo - Console.WriteLine("11%3 = " + (11 % 3)); // => 2 - - // Comparison operators - Console.WriteLine("3 == 2? " + (3 == 2)); // => false - Console.WriteLine("3 != 2? " + (3 != 2)); // => true - Console.WriteLine("3 > 2? " + (3 > 2)); // => true - Console.WriteLine("3 < 2? " + (3 < 2)); // => false - Console.WriteLine("2 <= 2? " + (2 <= 2)); // => true - Console.WriteLine("2 >= 2? " + (2 >= 2)); // => true - - // Bitwise operators! - /* - ~ Unary bitwise complement - << Signed left shift - >> Signed right shift - >>> Unsigned right shift - & Bitwise AND - ^ Bitwise exclusive OR - | Bitwise inclusive OR - */ - - // Incrementations - int i = 0; - Console.WriteLine("\n->Inc/Dec-rementation"); - Console.WriteLine(i++); //i = 1. Post-Incrementation - Console.WriteLine(++i); //i = 2. Pre-Incrementation - Console.WriteLine(i--); //i = 1. Post-Decrementation - Console.WriteLine(--i); //i = 0. Pre-Decrementation - - - /////////////////////////////////////// - // Control Structures - /////////////////////////////////////// - Console.WriteLine("\n->Control Structures"); - - // If statements are c-like - int j = 10; - if (j == 10) - { - Console.WriteLine("I get printed"); - } - else if (j > 10) - { - Console.WriteLine("I don't"); - } - else - { - Console.WriteLine("I also don't"); - } - - // Ternary operators - // A simple if/else can be written as follows - // <condition> ? <true> : <false> - string isTrue = (true) ? "True" : "False"; - Console.WriteLine("Ternary demo: " + isTrue); - - - // While loop - int fooWhile = 0; - while (fooWhile < 100) - { - //Console.WriteLine(fooWhile); - //Increment the counter - //Iterated 99 times, fooWhile 0->99 - fooWhile++; - } - Console.WriteLine("fooWhile Value: " + fooWhile); - - // Do While Loop - int fooDoWhile = 0; - do - { - //Console.WriteLine(fooDoWhile); - //Increment the counter - //Iterated 99 times, fooDoWhile 0->99 - fooDoWhile++; - } while (fooDoWhile < 100); - Console.WriteLine("fooDoWhile Value: " + fooDoWhile); - - // For Loop - int fooFor; - //for loop structure => for(<start_statement>; <conditional>; <step>) - for (fooFor = 0; fooFor < 10; fooFor++) - { - //Console.WriteLine(fooFor); - //Iterated 10 times, fooFor 0->9 - } - Console.WriteLine("fooFor Value: " + fooFor); - - // Switch Case - // A switch works with the byte, short, char, and int data types. - // It also works with enumerated types (discussed in Enum Types), - // the String class, and a few special classes that wrap - // primitive types: Character, Byte, Short, and Integer. - int month = 3; - string monthString; - switch (month) - { - case 1: - monthString = "January"; - break; - case 2: - monthString = "February"; - break; - case 3: - monthString = "March"; - break; - default: - monthString = "Some other month"; - break; - } - Console.WriteLine("Switch Case Result: " + monthString); - - - /////////////////////////////////////// - // Converting Data Types And Typcasting - /////////////////////////////////////// - - // Converting data - - // Convert String To Integer - // this will throw an Exception on failure - int.Parse("123");//returns an integer version of "123" - - // try parse will default to type default on failure - // in this case: 0 - int tryInt; - int.TryParse("123", out tryInt); - - // Convert Integer To String - // Convert class has a number of methods to facilitate conversions - Convert.ToString(123); - - /////////////////////////////////////// - // Classes And Functions - /////////////////////////////////////// - - Console.WriteLine("\n->Classes & Functions"); - - // (definition of the Bicycle class follows) - - // Use new to instantiate a class - Bicycle trek = new Bicycle(); - - // Call object methods - trek.speedUp(3); // You should always use setter and getter methods - trek.setCadence(100); - - // ToString is a convention to display the value of this Object. - Console.WriteLine("trek info: " + trek.ToString()); - - // Instantiate another new Bicycle - Bicycle octo = new Bicycle(5, 10); - Console.WriteLine("octo info: " + octo.ToString()); - - // Instantiate a new Penny Farthing - PennyFarthing funbike = new PennyFarthing(1, 10); - Console.WriteLine("funbike info: " + funbike.ToString()); - - Console.Read(); - } // End main method - - - } // End LearnCSharp class - - // You can include other classes in a .cs file - - - // Class Declaration Syntax: - // <public/private/protected> class <class name>{ - // //data fields, constructors, functions all inside. - // //functions are called as methods in Java. - // } - - public class Bicycle - { - // Bicycle's Fields/Variables - public int cadence; // Public: Can be accessed from anywhere - private int _speed; // Private: Only accessible from within the class - protected int gear; // Protected: Accessible from the class and subclasses - internal int wheels; // Internal: Accessible from within the assembly - string name; // default: Only accessible from within this class - - // readonly values are set at run time - // they can only be assigned upon declaration or in a constructor - readonly bool hasCardsInSpokes = false; // read-only private - - // Constructors are a way of creating classes - // This is a default constructor - public Bicycle() - { - gear = 1; - cadence = 50; - _speed = 5; - name = "Bontrager"; - } - - // This is a specified constructor (it contains arguments) - public Bicycle(int startCadence, int startSpeed, int startGear, string name, bool hasCardsInSpokes) - { - this.gear = startGear; - this.cadence = startCadence; - this._speed = startSpeed; - this.name = name; - this.hasCardsInSpokes = hasCardsInSpokes; - } - - // Constructors can be chained - public Bicycle(int startCadence, int startSpeed) : - this(startCadence, startSpeed, 0, "big wheels", true) - { - } - - // Function Syntax: - // <public/private/protected> <return type> <function name>(<args>) - - // classes can implement getters and setters for their fields - // or they can implement properties - - // Method declaration syntax: - // <scope> <return type> <method name>(<args>) - public int getCadence() - { - return cadence; - } - - // void methods require no return statement - public void setCadence(int newValue) - { - cadence = newValue; - } - - // virtual keyword indicates this method can be overridden - public virtual void setGear(int newValue) - { - gear = newValue; - } - - public void speedUp(int increment) - { - _speed += increment; - } - - public void slowDown(int decrement) - { - _speed -= decrement; - } - - // properties get/set values - // when only data needs to be accessed, consider using properties. - // properties may have either get or set, or both - private bool _hasTassles; // private variable - public bool hasTassles // public accessor - { - get { return _hasTassles; } - set { _hasTassles = value; } - } - - private int _frameSize; - public int FrameSize - { - get { return _frameSize; } - // you are able to specify access modifiers for either get or set - // this means only Bicycle class can call set on Framesize - private set { _frameSize = value; } - } - - //Method to display the attribute values of this Object. - public override string ToString() - { - return "gear: " + gear + - " cadence: " + cadence + - " speed: " + _speed + - " name: " + name + - " cards in spokes: " + (hasCardsInSpokes ? "yes" : "no") + - "\n------------------------------\n" - ; - } - } // end class Bicycle - - // PennyFarthing is a subclass of Bicycle - class PennyFarthing : Bicycle - { - // (Penny Farthings are those bicycles with the big front wheel. - // They have no gears.) - - // calling parent constructor - public PennyFarthing(int startCadence, int startSpeed) : - base(startCadence, startSpeed, 0, "PennyFarthing", true) - { - } - - public override void setGear(int gear) - { - gear = 0; - } - } + // Each .cs file should at least contain a class with the same name as the file + // you're allowed to do otherwise, but shouldn't for sanity. + public class LearnCSharp + { + // A console application must have a main method as an entry point + public static void Main(string[] args) + { + // Use Console.WriteLine to print lines + Console.WriteLine("Hello World"); + Console.WriteLine( + "Integer: " + 10 + + " Double: " + 3.14 + + " Boolean: " + true); + + // To print without a new line, use Console.Write + Console.Write("Hello "); + Console.Write("World"); + + + /////////////////////////////////////////////////// + // Types & Variables + // + // Declare a variable using <type> <name> + /////////////////////////////////////////////////// + + // Sbyte - Signed 8-bit integer + // (-128 <= sbyte <= 127) + sbyte fooSbyte = 100; + + // Byte - Unsigned 8-bit integer + // (0 <= byte <= 255) + byte fooByte = 100; + + // Short - Signed 16-bit integer + // (-32,768 <= short <= 32,767) + short fooShort = 10000; + + // Ushort - Unsigned 16-bit integer + // (0 <= ushort <= 65,535) + ushort fooUshort = 10000; + + // Integer - Signed 32-bit integer + // (-2,147,483,648 <= int <= 2,147,483,647) + int fooInt = 1; + + // Uinteger - Unsigned 32-bit integer + // (0 <= uint <= 4,294,967,295) + uint fooUint = 1; + + // Long - Signed 64-bit integer + // (-9,223,372,036,854,775,808 <= long <= 9,223,372,036,854,775,807) + long fooLong = 100000L; + // L is used to denote that this variable value is of type long or ulong + // anything without is treated as int or uint depending on size. + + // Ulong - Unsigned 64-bit integer + // (0 <= ulong <= 18,446,744,073,709,551,615) + ulong fooUlong = 100000L; + + // Float - Single-precision 32-bit IEEE 754 Floating Point + // Precision: 7 digits + float fooFloat = 234.5f; + // f is used to denote that this variable value is of type float; + // otherwise it is treated as double. + + // Double - Double-precision 64-bit IEEE 754 Floating Point + // Precision: 15-16 digits + double fooDouble = 123.4; + + // Bool - true & false + bool fooBoolean = true; + bool barBoolean = false; + + // Char - A single 16-bit Unicode character + char fooChar = 'A'; + + // Strings + string fooString = "My string is here!"; + Console.WriteLine(fooString); + + // formatting + string fooFs = string.Format("Check Check, {0} {1}, {0} {1:0.0}", 1, 2); + Console.WriteLine(fooFormattedString); + + // formatting dates + DateTime fooDate = DateTime.Now; + Console.WriteLine(fooDate.ToString("hh:mm, dd MMM yyyy")); + + // \n is an escaped character that starts a new line + string barString = "Printing on a new line?\nNo Problem!"; + Console.WriteLine(barString); + + // it can be written prettier by using the @ symbol + string bazString = @"Here's some stuff + on a new line!"; + Console.WriteLine(bazString); + + // quotes need to be escaped + // use \" normally + string quotedString = "some \"quoted\" stuff"; + Console.WriteLine(quotedString); + + // use "" when strings start with @ + string quotedString2 = @"some MORE ""quoted"" stuff"; + Console.WriteLine(quotedString2); + + // Use const or read-only to make a variable immutable + // const values are calculated at compile time + const int HOURS_I_WORK_PER_WEEK = 9001; + + // Nullable types + // any type can be made nullable by suffixing a ? + // <type>? <var name> = <value> + int? nullable = null; + Console.WriteLine("Nullable variable: " + nullable); + + // ?? is syntactic sugar for specifying default value + // in case variable is null + int notNullable = nullable ?? 0; + Console.WriteLine("Not nullable variable: " + notNullable); + + // Var - compiler will choose the most appropriate type based on value + var fooImplicit = true; + + /////////////////////////////////////////////////// + // Data Structures + /////////////////////////////////////////////////// + Console.WriteLine("\n->Data Structures"); + + // Arrays + // The array size must be decided upon declaration + // The format for declaring an array is follows: + // <datatype>[] <var name> = new <datatype>[<array size>]; + int[] intArray = new int[10]; + string[] stringArray = new string[1]; + bool[] boolArray = new bool[100]; + + // Another way to declare & initialize an array + int[] y = { 9000, 1000, 1337 }; + + // Indexing an array - Accessing an element + Console.WriteLine("intArray @ 0: " + intArray[0]); + + // Arrays are zero-indexed and mutable. + intArray[1] = 1; + Console.WriteLine("intArray @ 1: " + intArray[1]); // => 1 + + // Lists + // Lists are used more frequently than arrays as they are more flexible + // The format for declaring a list is follows: + // List<datatype> <var name> = new List<datatype>(); + List<int> intList = new List<int>(); + List<string> stringList = new List<string>(); + + // Another way to declare & initialize a list + List<int> z = new List<int> { 9000, 1000, 1337 }; + + // Indexing a list - Accessing an element + // Lists are zero-indexed and mutable. + Console.WriteLine("z @ 0: " + z[2]); + + // Lists don't default to a value; + // A value must be added before accessing the index + intList.Add(1); + Console.WriteLine("intList @ 0: " + intList[0]); + + + // Others data structures to check out: + // + // Stack/Queue + // Dictionary + // Read-only Collections + // Tuple (.Net 4+) + + + /////////////////////////////////////// + // Operators + /////////////////////////////////////// + Console.WriteLine("\n->Operators"); + + int i1 = 1, i2 = 2; // Shorthand for multiple declarations + + // Arithmetic is straightforward + Console.WriteLine("1+2 = " + (i1 + i2)); // => 3 + Console.WriteLine("2-1 = " + (i2 - i1)); // => 1 + Console.WriteLine("2*1 = " + (i2 * i1)); // => 2 + Console.WriteLine("1/2 = " + (i1 / i2)); // => 0 (0.5 truncated down) + + // Modulo + Console.WriteLine("11%3 = " + (11 % 3)); // => 2 + + // Comparison operators + Console.WriteLine("3 == 2? " + (3 == 2)); // => false + Console.WriteLine("3 != 2? " + (3 != 2)); // => true + Console.WriteLine("3 > 2? " + (3 > 2)); // => true + Console.WriteLine("3 < 2? " + (3 < 2)); // => false + Console.WriteLine("2 <= 2? " + (2 <= 2)); // => true + Console.WriteLine("2 >= 2? " + (2 >= 2)); // => true + + // Bitwise operators! + /* + ~ Unary bitwise complement + << Signed left shift + >> Signed right shift + >>> Unsigned right shift + & Bitwise AND + ^ Bitwise exclusive OR + | Bitwise inclusive OR + */ + + // Incrementations + int i = 0; + Console.WriteLine("\n->Inc/Dec-rementation"); + Console.WriteLine(i++); //i = 1. Post-Incrementation + Console.WriteLine(++i); //i = 2. Pre-Incrementation + Console.WriteLine(i--); //i = 1. Post-Decrementation + Console.WriteLine(--i); //i = 0. Pre-Decrementation + + + /////////////////////////////////////// + // Control Structures + /////////////////////////////////////// + Console.WriteLine("\n->Control Structures"); + + // If statements are c-like + int j = 10; + if (j == 10) + { + Console.WriteLine("I get printed"); + } + else if (j > 10) + { + Console.WriteLine("I don't"); + } + else + { + Console.WriteLine("I also don't"); + } + + // Ternary operators + // A simple if/else can be written as follows + // <condition> ? <true> : <false> + string isTrue = (true) ? "True" : "False"; + Console.WriteLine("Ternary demo: " + isTrue); + + + // While loop + int fooWhile = 0; + while (fooWhile < 100) + { + //Console.WriteLine(fooWhile); + //Increment the counter + //Iterated 99 times, fooWhile 0->99 + fooWhile++; + } + Console.WriteLine("fooWhile Value: " + fooWhile); + + // Do While Loop + int fooDoWhile = 0; + do + { + //Console.WriteLine(fooDoWhile); + //Increment the counter + //Iterated 99 times, fooDoWhile 0->99 + fooDoWhile++; + } while (fooDoWhile < 100); + Console.WriteLine("fooDoWhile Value: " + fooDoWhile); + + // For Loop + int fooFor; + //for loop structure => for(<start_statement>; <conditional>; <step>) + for (fooFor = 0; fooFor < 10; fooFor++) + { + //Console.WriteLine(fooFor); + //Iterated 10 times, fooFor 0->9 + } + Console.WriteLine("fooFor Value: " + fooFor); + + // Switch Case + // A switch works with the byte, short, char, and int data types. + // It also works with enumerated types (discussed in Enum Types), + // the String class, and a few special classes that wrap + // primitive types: Character, Byte, Short, and Integer. + int month = 3; + string monthString; + switch (month) + { + case 1: + monthString = "January"; + break; + case 2: + monthString = "February"; + break; + case 3: + monthString = "March"; + break; + default: + monthString = "Some other month"; + break; + } + Console.WriteLine("Switch Case Result: " + monthString); + + + /////////////////////////////////////// + // Converting Data Types And Typcasting + /////////////////////////////////////// + + // Converting data + + // Convert String To Integer + // this will throw an Exception on failure + int.Parse("123");//returns an integer version of "123" + + // try parse will default to type default on failure + // in this case: 0 + int tryInt; + int.TryParse("123", out tryInt); + + // Convert Integer To String + // Convert class has a number of methods to facilitate conversions + Convert.ToString(123); + + /////////////////////////////////////// + // Classes And Functions + /////////////////////////////////////// + + Console.WriteLine("\n->Classes & Functions"); + + // (definition of the Bicycle class follows) + + // Use new to instantiate a class + Bicycle trek = new Bicycle(); + + // Call object methods + trek.speedUp(3); // You should always use setter and getter methods + trek.setCadence(100); + + // ToString is a convention to display the value of this Object. + Console.WriteLine("trek info: " + trek.ToString()); + + // Instantiate another new Bicycle + Bicycle octo = new Bicycle(5, 10); + Console.WriteLine("octo info: " + octo.ToString()); + + // Instantiate a new Penny Farthing + PennyFarthing funbike = new PennyFarthing(1, 10); + Console.WriteLine("funbike info: " + funbike.ToString()); + + Console.Read(); + } // End main method + + + } // End LearnCSharp class + + // You can include other classes in a .cs file + + + // Class Declaration Syntax: + // <public/private/protected> class <class name>{ + // //data fields, constructors, functions all inside. + // //functions are called as methods in Java. + // } + + public class Bicycle + { + // Bicycle's Fields/Variables + public int cadence; // Public: Can be accessed from anywhere + private int _speed; // Private: Only accessible from within the class + protected int gear; // Protected: Accessible from the class and subclasses + internal int wheels; // Internal: Accessible from within the assembly + string name; // default: Only accessible from within this class + + // readonly values are set at run time + // they can only be assigned upon declaration or in a constructor + readonly bool hasCardsInSpokes = false; // read-only private + + // Constructors are a way of creating classes + // This is a default constructor + public Bicycle() + { + gear = 1; + cadence = 50; + _speed = 5; + name = "Bontrager"; + } + + // This is a specified constructor (it contains arguments) + public Bicycle(int startCadence, int startSpeed, int startGear, + string name, bool hasCardsInSpokes) + { + this.gear = startGear; + this.cadence = startCadence; + this._speed = startSpeed; + this.name = name; + this.hasCardsInSpokes = hasCardsInSpokes; + } + + // Constructors can be chained + public Bicycle(int startCadence, int startSpeed) : + this(startCadence, startSpeed, 0, "big wheels", true) + { + } + + // Function Syntax: + // <public/private/protected> <return type> <function name>(<args>) + + // classes can implement getters and setters for their fields + // or they can implement properties + + // Method declaration syntax: + // <scope> <return type> <method name>(<args>) + public int getCadence() + { + return cadence; + } + + // void methods require no return statement + public void setCadence(int newValue) + { + cadence = newValue; + } + + // virtual keyword indicates this method can be overridden + public virtual void setGear(int newValue) + { + gear = newValue; + } + + public void speedUp(int increment) + { + _speed += increment; + } + + public void slowDown(int decrement) + { + _speed -= decrement; + } + + // properties get/set values + // when only data needs to be accessed, consider using properties. + // properties may have either get or set, or both + private bool _hasTassles; // private variable + public bool hasTassles // public accessor + { + get { return _hasTassles; } + set { _hasTassles = value; } + } + + private int _frameSize; + public int FrameSize + { + get { return _frameSize; } + // you are able to specify access modifiers for either get or set + // this means only Bicycle class can call set on Framesize + private set { _frameSize = value; } + } + + //Method to display the attribute values of this Object. + public override string ToString() + { + return "gear: " + gear + + " cadence: " + cadence + + " speed: " + _speed + + " name: " + name + + " cards in spokes: " + (hasCardsInSpokes ? "yes" : "no") + + "\n------------------------------\n" + ; + } + } // end class Bicycle + + // PennyFarthing is a subclass of Bicycle + class PennyFarthing : Bicycle + { + // (Penny Farthings are those bicycles with the big front wheel. + // They have no gears.) + + // calling parent constructor + public PennyFarthing(int startCadence, int startSpeed) : + base(startCadence, startSpeed, 0, "PennyFarthing", true) + { + } + + public override void setGear(int gear) + { + gear = 0; + } + } } // End Namespace ``` diff --git a/ko-kr/lua-kr.html.markdown b/ko-kr/lua-kr.html.markdown index 3f6223d6..2badf734 100644 --- a/ko-kr/lua-kr.html.markdown +++ b/ko-kr/lua-kr.html.markdown @@ -1,4 +1,4 @@ ---- +--- language: lua category: language contributors: @@ -395,6 +395,7 @@ g() -- 343이 출력됩니다. 그전까지는 아무것도 출력되지 않습 --]] ``` + ## 참고자료 루아를 배우는 일이 흥미진진했던 이유는 <a href="http://love2d.org/">Love 2D 게임 엔진</a>을 이용해 @@ -420,4 +421,4 @@ learn.lua로 저장한 후 "lua learn.lua"를 실행해 보세요! 이 글은 tylerneylon.com에 처음으로 써본 글이며, <a href="https://gist.github.com/tylerneylon/5853042">Github의 Gist</a>에서도 확인할 수 있습니다. -루아로 즐거운 시간을 보내세요!
\ No newline at end of file +루아로 즐거운 시간을 보내세요! diff --git a/objective-c.html.markdown b/objective-c.html.markdown new file mode 100644 index 00000000..2b1b3c67 --- /dev/null +++ b/objective-c.html.markdown @@ -0,0 +1,310 @@ +--- + +language: Objective-C +contributors: + - ["Eugene Yagrushkin", "www.about.me/yagrushkin"] + - ["Yannick Loriot", "https://github.com/YannickL"] +filename: LearnObjectiveC.m + +--- + +Objective-C is the main programming language used by Apple for the OS X and iOS operating systems and their respective frameworks, Cocoa and Cocoa Touch. +It is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. + +```cpp +// Single-line comments start with // + +/* +Multi-line comments look like this. +*/ + +// Imports the Foundation headers with #import +#import <Foundation/Foundation.h> +#import "MyClass.h" + +// Your program's entry point is a function called +// main with an integer return type. +int main (int argc, const char * argv[]) +{ + // Create an autorelease pool to manage the memory into the program + NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; + + // Use NSLog to print lines to the console + NSLog(@"Hello World!"); // Print the string "Hello World!" + + /////////////////////////////////////// + // Types & Variables + /////////////////////////////////////// + + // Primitive declarations + int myPrimitive1 = 1; + long myPrimitive2 = 234554664565; + + // Object declarations + // Put the * in front of the variable names for strongly-typed object declarations + MyClass *myObject1 = nil; // Strong typing + id myObject2 = nil; // Weak typing + // %@ is an object + // 'description' is a convention to display the value of the Objects + NSLog(@"%@ and %@", myObject1, [myObject2 description]); // Print "(null) and (null)" + + // String + NSString *worldString = @"World"; + NSLog(@"Hello %@!", worldString); // Print "Hello World!" + + // Character literals + NSNumber *theLetterZNumber = @'Z'; + char theLetterZ = [theLetterZNumber charValue]; + NSLog(@"%c", theLetterZ); + + // Integral literals + NSNumber *fortyTwoNumber = @42; + int fortyTwo = [fortyTwoNumber intValue]; + NSLog(@"%i", fortyTwo); + + NSNumber *fortyTwoUnsignedNumber = @42U; + unsigned int fortyTwoUnsigned = [fortyTwoUnsignedNumber unsignedIntValue]; + NSLog(@"%u", fortyTwoUnsigned); + + NSNumber *fortyTwoShortNumber = [NSNumber numberWithShort:42]; + short fortyTwoShort = [fortyTwoShortNumber shortValue]; + NSLog(@"%hi", fortyTwoShort); + + NSNumber *fortyTwoLongNumber = @42L; + long fortyTwoLong = [fortyTwoLongNumber longValue]; + NSLog(@"%li", fortyTwoLong); + + // Floating point literals + NSNumber *piFloatNumber = @3.141592654F; + float piFloat = [piFloatNumber floatValue]; + NSLog(@"%f", piFloat); + + NSNumber *piDoubleNumber = @3.1415926535; + piDouble = [piDoubleNumber doubleValue]; + NSLog(@"%f", piDouble); + + // BOOL literals + NSNumber *yesNumber = @YES; + NSNumber *noNumber = @NO; + + // Array object + NSArray *anArray = @[@1, @2, @3, @4]; + NSNumber *thirdNumber = anArray[2]; + NSLog(@"Third number = %@", thirdObject); // Print "Third number = 3" + + // Dictionary object + NSDictionary *aDictionary = @{ @"key1" : @"value1", @"key2" : @"value2" }; + NSObject *valueObject = aDictionary[@"A Key"]; + NSLog(@"Object = %@", valueObject); // Print "Object = (null)" + + /////////////////////////////////////// + // Operators + /////////////////////////////////////// + + // The operators works like in the C language + // For example: + 2 + 5; // => 7 + 4.2f + 5.1f; // => 9.3f + 3 == 2; // => 0 (NO) + 3 != 2; // => 1 (YES) + 1 && 1; // => 1 (Logical and) + 0 || 1; // => 1 (Logical or) + ~0x0F; // => 0xF0 (bitwise negation) + 0x0F & 0xF0; // => 0x00 (bitwise AND) + 0x01 << 1; // => 0x02 (bitwise left shift (by 1)) + + /////////////////////////////////////// + // Control Structures + /////////////////////////////////////// + + // If-Else statement + if (NO) + { + NSLog(@"I am never run"); + } else if (0) + { + NSLog(@"I am also never run"); + } else + { + NSLog(@"I print"); + } + + // Switch statement + switch (2) + { + case 0: + { + NSLog(@"I am never run"); + } break; + case 1: + { + NSLog(@"I am also never run"); + } break; + default: + { + NSLog(@"I print"); + } break; + } + + // While loops statements + int ii = 0; + while (ii < 4) + { + NSLog(@"%d,", ii++); // ii++ increments ii in-place, after using its value. + } // => prints "0," + // "1," + // "2," + // "3," + + // For loops statements + int jj; + for (jj=0; jj < 4; jj++) + { + NSLog(@"%d,", ii++); + } // => prints "0," + // "1," + // "2," + // "3," + + // Foreach statements + NSArray *values = @[@0, @1, @2, @3]; + for (NSNumber *value in values) + { + NSLog(@"%@,", value); + } // => prints "0," + // "1," + // "2," + // "3," + + // Try-Catch-Finally statements + @try + { + // Your statements here + @throw [NSException exceptionWithName:@"FileNotFoundException" + reason:@"File Not Found on System" userInfo:nil]; + } @catch (NSException * e) + { + NSLog(@"Exception: %@", e); + } @finally + { + NSLog(@"Finally"); + } // => prints "Exception: File Not Found on System" + // "Finally" + + /////////////////////////////////////// + // Objects + /////////////////////////////////////// + + // Create an object instance by allocating memory and initializing it. + // An object is not fully functional until both steps have been completed. + MyClass *myObject = [[MyClass alloc] init]; + + // The Objective-C model of object-oriented programming is based on message + // passing to object instances. + // In Objective-C one does not simply call a method; one sends a message. + [myObject instanceMethodWithParameter:@"Steve Jobs"]; + + // Clean up the memory you used into your program + [pool drain]; + + // End the program + return 0; +} + +/////////////////////////////////////// +// Classes And Functions +/////////////////////////////////////// + +// Declare your class in a header(MyClass.h) file: +// Class Declaration Syntax: +// @interface ClassName : ParentClassName <ImplementedProtocols> +// { +// Member variable declarations; +// } +// -/+ (type) Method declarations; +// @end +@interface MyClass : NSObject <MyCustomProtocol> +{ + int count; + id data; + NSString *name; +} +// Convenience notation to auto generate public getter and setter +@property int count; +@property (copy) NSString *name; // Copy the object during assignment. +@property (readonly) id data; // Declare only a getter method. + +// Methods ++/- (return type)methodSignature:(Parameter Type *)parameterName; + +// + for class method ++ (NSString *)classMethod; + +// - for instance method +- (NSString *)instanceMethodWithParmeter:(NSString *)string; +- (NSNumber *)methodAParameterAsString:(NSString*)string andAParameterAsNumber:(NSNumber *)number; + +@end + +// Implement the methods in an implementation (MyClass.m) file: + +@implementation UserObject + +// Call when the object is releasing +- (void)dealloc +{ +} + +// Constructors are a way of creating classes +// This is a default constructor which is call when the object is creating +- (id)init +{ + if ((self = [super init])) + { + self.count = 1; + } + return self; +} + ++ (NSString *)classMethod +{ + return [[self alloc] init]; +} + +- (NSString *)instanceMethodWithParmeter:(NSString *)string +{ + return @"New string"; +} + +- (NSNumber *)methodAParameterAsString:(NSString*)string andAParameterAsNumber:(NSNumber *)number +{ + return @42; +} + +// Methods declared into MyProtocol +- (void)myProtocolMethod +{ + // statements +} + +@end + +/* + * A protocol declares methods that can be implemented by any class. + * Protocols are not classes themselves. They simply define an interface + * that other objects are responsible for implementing. + */ +@protocol MyProtocol + - (void)myProtocolMethod; +@end + + + +``` +## Further Reading + +[Wikipedia Objective-C](http://en.wikipedia.org/wiki/Objective-C) + +[Learning Objective-C](http://developer.apple.com/library/ios/referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/) + +[iOS For High School Students: Getting Started](http://www.raywenderlich.com/5600/ios-for-high-school-students-getting-started) diff --git a/zh-cn/racket-cn.html.markdown b/zh-cn/racket-cn.html.markdown index e17aedda..d43511ea 100644 --- a/zh-cn/racket-cn.html.markdown +++ b/zh-cn/racket-cn.html.markdown @@ -1,6 +1,7 @@ --- language: racket +lang: zh-cn filename: learnracket.rkt contributors: - ["th3rac25", "https://github.com/voila"] |