[Java] Basic Learning Example

Source: Internet
Author: User
Tags abstract return
Long time no update, lazy, the fight!
1, this is a basic file operation, the implementation of the file read, write a number of operations
Package trying;
Import java.io.*;

/**
* @author gooing
*/
public class Filerw {
Private file F = new file ("D:\\j2\\a.txt");
public int Getnum () {
int i =-1;
try{
String stri= "";
BufferedReader in = new BufferedReader (new FileReader (f));
while ((Stri=in.readline ())!=null) {
i = Integer.parseint (Stri.trim ());
}
In.close ();
}catch (Exception e) {
E.printstacktrace ();
}
return i;
}
public void Setnum () {
int i = Getnum ();
i++;
try{
PrintWriter out=new PrintWriter (New BufferedWriter) (New FileWriter (F,false));
Out.write (string.valueof (i));
May be the reason for the encoding, if you write int directly, there will be Java encoding and Windows encoding confusion, so write here is a string
Out.close ();
}catch (Exception e) {
E.printstacktrace ();
}
}
public static void Main (string[] args) {
Filerw FRW = new Filerw ();
for (int i =0;i<100; i++) {
Frw.setnum ();
System.out.println (Frw.getnum ());
}
}
}

2, the following is an example of learning the factory model
Garden.java implements an abstract garden factory where you can get the garden's central plants and surrounding plants;
Of course, for different gardens, the central plant and the surrounding plants are not the same
Package pkgfactory;

/**
* @author gooing TODO To change the template for this generated type
* Comment Go to window-preferences-java-code style-code
* Templates
*/
Public abstract class Garden {
Public abstract Plant getcenter ();
Public abstract Plant Getborder ();

public void Memo () {
System.out.println ("centerplant:" + getcenter (). GetName ());
System.out.println ("borderplant:" + getborder (). GetName ());
System.out.println ("---------------------");
}
}

Plant.java realizes the basic abstraction of plants in the garden, where only one plant name attribute is provided.
Package pkgfactory;
public class Plant {
Public plant (String name) {
THIS.name = name;
}

Public String GetName () {
return name;
}

private String name;
}

Vergarden.java and Flowergarden.java each realized a vegetable garden and gardens
Package pkgfactory;
public class Veggarden extends Garden {
Public Plant Getcenter () {
return new plant ("wheat");
}

Public Plant Getborder () {
return new plant ("carrot");
}
}

Package pkgfactory;
public class Flowergarden extends Garden {
Public Plant Getcenter () {
return new plant ("Rose");
}

Public Plant Getborder () {
return new plant ("Juhua");
}
}

Gardener.java is a driver of this routine
Package pkgfactory;
public class Gardener {
public static void Main (string[] args) {
Garden G1 = new Flowergarden ();
Garden g2 = new Veggarden ();
G1.memo ();
G2.memo ();
}
}






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.