J2SE Object-oriented _ overloaded _this_static_package_import_ inheritance _ access control

Source: Internet
Author: User
Tags gety

Overloading of methods

Constructing method Overloads

creation and use of objects

classPoint {Private Doublex; Private Doubley; Point (DoubleX1,Doubley1) {x=X1; Y=Y1; }     Public DoubleGetX () {returnx;}  Public DoubleGetY () {returny;}  Public voidSetX (Doublei) {x =i;}  Public voidSety (Doublei) {y =i;}}classCircle {PrivatePoint o; Private Doubleradius; Circle (point P,Doubler) {o=p; Radius=R; } Circle (Doubler) {o=NewPoint (0.0, 0.0); Radius=R; }        Booleancontains (point p) {Doublex = P.getx ()-O.getx (); Doubley = p.gety ()-o.gety (); if(x*x + y*y > Radius * radius)return false; Else return true; }             Public voidSetO (DoubleXDoubley) {o.setx (x);    O.sety (y); }     PublicPoint Geto () {returno;}  Public DoubleGetradius () {returnradius;}  Public voidSetradius (Doubler) {radius =R;}  Public DoubleArea () {return3.14 * RADIUS *radius; }} Public classtestcircle { Public Static voidMain (String args[]) {Circle C1=NewCircle (NewPoint (1.0,2.0), 2.0); Circle C2=NewCircle (5.0); System.out.println ("C1: (" +c1.geto (). GetX () + "," +c1.geto (). GetY () + ")," +C1.getradius ()); System.out.println ("C2: (" +C2.geto (). GetX ()+ "," +c2.geto (). GetY () + ")," +C2.getradius ()); System.out.println ("C1 area =" +C1.area ()); System.out.println ("C1 area =" +C2.area ()); C1.seto (5,6); C2.setradius (9.0); System.out.println ("C1: (" +c1.geto (). GetX () + "," +c1.geto (). GetY () + ")," +C1.getradius ()); System.out.println ("C2: (" +c2.geto (). GetX () + "," +c2.geto (). GetY () + ")," +C2.getradius ()); System.out.println ("C1 area =" +C1.area ()); System.out.println ("C1 area =" +C2.area ()); Point P1=NewPoint (5.2, 6.3);        System.out.println (C1.contains (p1)); System.out.println (C1.contains (NewPoint (10.0,9.0))); }}
Testcircle.javaThis keyword

Static keyword

 Public classCat {Private Static intSid = 0; PrivateString name; intID; Cat (String name) { This. Name =name; ID= sid++; }     Public voidinfo () {System.out.println ("My name is" +name+ "No." +ID); }     Public Static voidMain (String arg[]) {Cat.sid= 100; Cat Mimi=NewCat ("Mimi"); Mimi.sid= 2000; Cat Pipi=NewCat ("Pipi");         Mimi.info ();    Pipi.info (); }}
Cat.javaPackage and Import statements

Introduction to the main packages in J2SDK

class inheritance and permission control

classPerson {PrivateString name; Private intAge ;  Public voidsetName (String name) { This. name=name; }     Public voidSetage (intAge ) {         This. age=Age ; }     PublicString GetName () {returnname; }     Public intGetage () {returnAge ; }}classStudentextendsPerson {PrivateString School;  PublicString Getschool () {returnSchool; }     Public voidSetschool (String school) { This. School =School; } } Public classTest { Public Static voidMain (String arg[]) {Student Student=NewStudent (); Student.setname ("John"); Student.setage (18); Student.setschool ("SCH");        System.out.println (Student.getname ());        System.out.println (Student.getage ());    System.out.println (Student.getschool ()); }}
Testperson.javaAccess Control

 Public classtestaccess {}classT {Private inti = 0; intj = 0; protected intK = 0;  Public intm = 0;  Public voidm () {i= 9; }}classTT { Public voidm () {T T=NewT ();    System.out.println (T.J); }}
Testaccess.java

J2SE Object-oriented _ overloaded _this_static_package_import_ inheritance _ access control

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.