Java

265 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
265
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Java

  1. 1. java
  2. 2. OOP•• Problem Space)
  3. 3. class) object)• scripting language Perl PHP• class members) properties) member functions) class { ... } class constructor new class constructor
  4. 4. • static instance instance
  5. 5. • MyClass n x n static Method 1 getX() instance•• MyClass a = new MyClass();• MyClass b = new MyClass();
  6. 6. • instance x instance x instance) n static instance n reference MyClass n n Class Variable" n MyClass.n instance
  7. 7. • Method reference Method Method MyClass) instance Method instance reference Method static
  8. 8. • static Method• static Method Method instance• printCircle TestPass1 instance printCircle static
  9. 9. constructor)
  10. 10. constructor)• constructor) new
  11. 11. • //DriveAcar05.java class Vehicle { int numberOfWheels ; boolean hasEngine ; //constructor Vehicle() { // (1) numberOfWheels = 6 ; hasEngine = true ; } void run() { System.out.println("I am running") ; } } public class DriveAcar05 { public static void main(String[] args) { Vehicle myCar = new Vehicle() ; System.out.println("mycar has "+myCar.numberOfWheels+"Wheels.") ; // (2) System.out.println("That mycar has an engin is "+myCar.hasEngine+".") ; // (3) } }
  12. 12. • void• DriveAcar05.java main() Vehicle Vehicle myCar = new Vehicle(); Vehicle() numberOfWheels = 6 hasEngine =
  13. 13.
  14. 14. • //DriveAcar06.java class Vehicle { int numberOfWheels ; boolean hasEngine ; //constructor Vehicle() { numberOfWheels = 6 ; hasEngine = true ; System.out.println("A car has been built") ; // (1) notice this line } void run() { System.out.println("I am running") ; } } public class DriveAcar06 { public static void main(String[] args) { Vehicle myCar = new Vehicle() ; System.out.println("mycar has "+myCar.numberOfWheels+"Wheels.") ; // (2) System.out.println("That mycar has an engin is "+myCar.hasEngine+".") ; // (3) } }
  15. 15. Overloading Constructor)•
  16. 16. • //OverloadConstructor.java class Vehicle { int numberOfWheels ; boolean hasEngine ; //constructor and Overload Constructor Vehicle() { numberOfWheels = 6 ; hasEngine = true ; System.out.println("A car has been built") ; //notice this line } Vehicle(int number, boolean engine) { // (1) numberOfWheels = number ; hasEngine = engine ; System.out.println("A car has been built") ; //notice this line } void run() { System.out.println("I am running") ; } } public class OverloadConstructor { public static void main(String[] args) { Vehicle myCar = new Vehicle(4,true) ; // (2) System.out.println("mycar has "+myCar.numberOfWheels+"Wheels.") ; System.out.println("That mycar has an engin is "+myCar.hasEngine+".") ; } }
  17. 17. • //TestThis.java this() class A { int a ; A() { a=0; System.out.println("Default constructor") ; } A(int i) { this() ; a=i; System.out.println("constructor 1 ") ; } A(int i, int j) { this(i+j) ; System.out.println("constructor 2 ") ;
  18. 18. • } } public class DriveAcar06 { public static void main(String[] args) { System.out.println("Instantiate x"); A x = new A() ; System.out.println("Variable a is "+ x.a) ; System.out.println("Instantiate y "); A y = new A(5) ; System.out.println("Variable a is "+ y.a) ; System.out.println("Instantiate z "); A z = new A(5,6) ; System.out.println("Variable a is "+ z.a) ; } } this()
  19. 19. • static File System File System File Access• File Access Component• Set Fso= CreateObject("Scripting.FileSystemObject")• Fso File Access Component Fso• Open Text File Method•• Object.openTextFile(filename[,iomode][,create][,Format]•• Object
  20. 20. • Filename• ( Required)• iomode• ( Optional)• ForReading(1)• ForWriting(2)• ForAppending(8)• Create• ( optional)• True• False Error False
  21. 21. • Format• ( optional) Tristate UseDefault(-2) ASCII ASCII• Tristate True(-1) Unicode• Tristate False(0) ASCII• ASCII•• Set fso= CreateObject("Scripting.FileSystemObject")• Set Myfile=fso.openTextfile("c:testfile.txt",1, True)• fso File Access fso OpenTextFile testfile.txt• OpenTextFile MyFile Text Stream Object• TextStream Object ( Text) TextStream
  22. 22. • Close• TextStream Object• Read•• ReadAll•• ReadLine•• Skip•• SkipLine•
  23. 23. • Write•• WriteLine•• WriteBlankLines•
  24. 24. • Sample1.asp• <% if request.form("message") = "" then %>• <html>• <body>• <form action="sample1.asp" method="post">• <font size=+1> text</font><br>• Name:• <input type=text name=message size=30>• <input type=submit value=• form>
  25. 25. • <%• else• Set FileObject = Server.CreateObject("Scripting.FileSystemObject")• MsgFile = Server.MapPath ("Text.txt")• Set OutStream= FileObject.CreateTextFile (MsgFile, True)• OutStream.WriteLine Request.Form("message")• Set OutStream = Nothing• Set MessageStr =Nothing• %>• a href="sample2.asp">sample2.asp</a>• body>• </html>• <% end if %>•• Out Put
  26. 26. • Sample1.asp•• <html>• <body>• br>• <%• Set FileObject = Server.CreateObject("Scripting.FileSystemObject")• MsgFile = Server.MapPath ("Text.txt")• On Error Resume Next• Set InStream= FileObject.OpenTextFile (MSGFile, 1, False, False)• Response.Write Instream.ReadALL & "<BR>"• Set Instream=Nothing• %>• </body>• </html>••• Out Put•

×