The first week of Java learning

Source: Internet
Author: User

In the first week, I learned about the installation of the JDK and the configuration of the environment, and initially learned about the differences between Java and C, and learned about Java variables, basic data types, and object-oriented foundations. And I have done some simple Java program writing.

(1) Learn why abstract classes are used, and abstract classes are used so that programmers do not forget to replicate when they inherit.

Abstract class Printer

{

void Open ()

{

System.out.println ("open");

}

void Close ()

{

System.out.println ("close");

}

abstract void print ();//Use abstraction to make the programmer forget the replication or it will go wrong}

Class Hpprinter extends Printer

{

void print ()//Use abstract class when this must be replicated, otherwise it will be an error

{

SYSTEM.OUT.PRINTLN ("Use inkjet printer");

}

}

Class Cannonprinter extends Printer

{

void print ()//Use abstract class when this must be replicated, otherwise it will be an error

{

System.out.println ("Using a stylus printer");

}

}

Class Test

{

public static void Main (String args[])

{

Printer P1 = new Hpprinter (); Move Down

P1.open ();

P1.close ();

P1.print ();

Printer P2 = new Cannonprinter (); Move Down

P2.open ();

P2.close ();

P2.print ();

}

}

(2) The call of the parent class is learned as far as possible, the function of the class is attributed to the parent class, the invocation is convenient, and it is easy to modify at any time

Class Printer

{

void Open ()

{

System.out.println ("open");

}

void Close ()

{

System.out.println ("close");

}

void print (String s)

{

System.out.println ("print->" + s);

}

}

Class Hpprinter extends Printer

{

}

Class Cannonprinter extends Printer

{

void Close ()

{

This.clean ();

Super.close ();

}

void Clean ()

{

System.out.println ("clean");

}

}

Class Text

{

public static void Main (String args[])

{

int flag = 1;

if (flag = = 0)

{

Hpprinter hpprinter = new Hpprinter ();

Hpprinter.open ();

Hpprinter.print ("abc");

Hpprinter.close ();

}

else if (flag = = 1)

{

Cannonprinter cannonprinter = new Cannonprinter ();

Cannonprinter.open ();

Cannonprinter.print ("abc");

Cannonprinter.close ();

}

}

}

The first week of Java learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.