Java Novice Note 10 builder

Source: Internet
Author: User

1. Small program of Draw award

Package Com.yfs.javase;import Java.io.ioexception;import Java.nio.charbuffer;import Java.util.random;public class DEMO1 {/** * Analog draw award */public static void Main (string[] args) {random ran = new Random (); int[] A = new int[7];  System.out.println ("Start of the draw:");//Generate award for (int i = 0; i < a.length; i++) {A[i] = Ran.nextint (33) + 1;//Find duplicates for (int j = 0; J < I; J + +) {if (a[j] = = A[i]) {System.out.println ("+" + (i + 1) + "bit" + a[j] + "+" + (j + 1) + "number Same");    i--;//to repeat the number of}}}system.out.println ("The result of this draw:");//Output award for (int i = 0; i < a.length; i++) {System.out.print (a[i] + "\ t");}}

2. Constructors

Package Com.yfs.javase;public class Constructiondemo {public int b = 10;/** * Construction method * 1. If the class does not provide a constructor method  * Compile-time system to automatically add an argument-free construction method  * 2. Constructor method to instantiate object  * 3. Class declaration Construction method The system no longer provides a constructor method * 4. No return value type declaration has the same name as the class * 5. The form of the instantiated object is determined by the construction method */public Constructiondemo () {Syst Em.out.println ("Create an Object");}    Constructor method overloads public Constructiondemo (String msg) {System.out.println ("constructor with arguments   " +  msg);} Public Constructiondemo (Double A, int[] b) {System.out.println ("construction method with 2 parameters   " +  a);//execute output for (int i = 0; i < B.L Ength; i++) {System.out.println (b[i]);}} Common method public void Constructiondemo () {System.out.println ("method with return value ...) Whether to create an object "); public static void Main (string[] args) {Constructiondemo cs = new Constructiondemo ();//Call construction method}public void Test (int a) {Sy STEM.OUT.PRINTLN ("Call Test () ....");}}

3. Constructor test

Package Com.yfs.javase;public class ConTest {public static void main (string[] args) {Constructiondemo  CSD = new Constr Uctiondemo ();        Csd.test (a);        Call the constructor method with parameters to instantiate the object        constructiondemo  c1 = new Constructiondemo ("Hello everyone! ");                        Constructiondemo  c2 = new Constructiondemo (a);        Constructiondemo  c2 = new Constructiondemo (new int[]{1,2,3});}}

4.Person class

The package com.yfs.javase;//constructs a method using this to call public class Person {String name;int age;char sex;boolean married;public person ( {System.out.println ("Create person object"); speak ();//Call other methods}public person (String N) {//person ();//this ();/ This invokes the constructor method without arguments this ("Harry", 25, ' female ', true);//must be the first sentence of the construction method code System.out.println ("Call person (name) ...");} The constructor initializes the property to the public person (String N, int A, char s, boolean m) {name = n;//the passed-in parameter is assigned to the attribute age  = A;sex = s;married = m; System.out.println ("Person (String N, int A, char s, boolean m)");} public void Speak () {System.out.println (name + "Are you human?") ");} public void Sleep () {System.out.println (name +) did you sleep? Speak ();//Call other methods//person ();//Other methods cannot call construction method}}

Class 5.Person Test 1

Package Com.yfs.javase;public class Persontest {public static void main (string[] args) {person P1 = new Person ("Zhang San", 20, ' Male ', false); System.out.println (P1.name); System.out.println (P1.age); System.out.println (P1.sex); System.out.println (p1.married);p 1.speak ();p 1.sleep (); person P2 = new Person ();}}

Class 6.Person 2

Package Com.yfs.javase;public class Person {String name;int age;char sex;boolean married;public person () { System.out.println ("Create person Object");} Public person (String name) {//Parameter name has the same name as the property  name local variable this.name = name;//Property This represents the current object System.out.println ("Call person" + Name+ ") ..."); public void Speak () {System.out.println (this.name +) Are you really human? "); System.out.println (this);} public void Sleep () {System.out.println (name +) did you sleep? ");} Destroying an object is the method called @overrideprotected Void Finalize () throws Throwable {System.out.println (name  + "object is destroyed ...");}

7.Person Class 2 Test (garbage collection)

Package Com.yfs.javase;public class PersonTest1 {public static void main (string[] args) {person P1 = new Person ("John Doe");//Object Life from Newp1.speak (); person P2 = new Person ();p 2.speak (); Person P3 = NULL;P3 = new Person ("Zhang San");p 3 = null;//Life End System.GC ();//Forced Recycle System.out.println ("Program execution End ...");}}

Java Novice Note 10 builder

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.