Object-oriented experiment report two

Source: Internet
Author: User

The second experiment report of object-oriented analysis and design

A. Different types of methods for the class , the visibility of the properties

 no

td>

Visible/accessibility

In the same class

In the same package

  in different packages;

p> the same bun class  

Different buns category  

 public

 yes

/td>

 yes

 yes

 yes

 yes

 protected 

 yes

 yes

 no< /p>

 yes

 yes

 package 

 yes

 yes

 no

 yes

 no

 private

 yes

 no

 no

 no

Public class Student {

Public String home;

protected int age;

Private String name;

Private String Banji;

Student ()

{

Home = "China";

}

Student (int age,string name,string home,string Banji) {//constructor

this. Age = age;

this. Name = name;

this. Banji = Banji;

this. Home = home;

}

Public void Say ()

{

System. out. println ("I call" +name+ "this year" +age+ "Years" + "class" +banji);

}

}

Defines a class Student

Yes

Public Property Home

Protected attribute Age

Private property name Banji

Public method Say ()

1. Attribute methods of the same class are visible regardless of type.

2. Classes defined in the same package are not visible except for private type properties, and others are visible.

Declaring an object in the same package

Student S1 = new Student ();

Cannot access private type such as: S1.name S1.banji is wrong

3. Classes defined in different packages are visible only for properties of the public type

Classes in different packages can access the properties and methods of public in a class

TestClass T1 = new TestClass ();

T1.id = "1";

T1.age private type is not accessible

T1.name protected type also cannot access

4. Sub-classes in the same package, except private type not visible, others are visible

5. Different bun classes only public and protected types of attribute methods are visible

two. Inheriting and overriding method Properties of a parent class function

Public class Student2 extends student{

/**

* Override Properties

* Public String home;

protected int age;

private String name;

Private String Banji;

*/

Public void Say () {// override method

System. out. println ("This is a subclass of overloaded say" + "age" +age+ "from" +home);

}

Student2 ()//Sub-class construction method

{

Super ();

System. out. println ("Subclasses have customized their own construction methods");

Name = "hyf"; Same as Bun class cannot access members of the private definition in the parent class

Age = 80; The same bun class can access members defined by the protected in the parent class

Home = "Chengdu";

}

}

Three Conversions between data types

Data types in Java and conversions between them

Basic data types

There are four basic types of the following:
1) int length data types are: Byte (8bits), short (16bits), int (32bits), Long (64bits),
2) Float length data types are: Single precision (32bits float), double precision (64bits double), in Java the decimal is the default double type, to define the float need to be declared after the data with F;
3) The value of a Boolean variable is: Ture, False
4) Char data types are: Unicode characters, 16 bits
Corresponding class types: Integer, Float, Boolean, Character, Double, short, Byte, Long

The basic data types are from low to advanced, respectively:

( byte , Short , Char )-- int -- Long -- float -- Double

PS: The "level" here refers to the size of the range that represents the value.

Transition between data types

It is divided into the following situations:

1) Low to advanced automatic type conversion;

2) advanced to low-level forced type conversions (which can result in overflow or loss of precision);

3) Basic type conversion to class type;

4) Conversion of basic types to strings;

5) class type to string conversion

Conversion rules between basic data types


1. In arithmetic expressions such as a double operand and a bitwise operation, the low-level data type is automatically converted to an advanced data type based on the type of the operand, divided into the following situations:

1) As long as one of the two operands is of type double, the other is converted to a double type, and the result is also a double type;

2) As long as one of the two operands is of type float, the other will be converted to float type, and the result is a float type;

3) As long as one of the two operands is long, the other is converted to a long type, and the result is a long type;

4) Two operands (including Byte, short, int, char) are converted to the int type, and the result is of type int.

2. If the low-level type is char, converting to the Advanced type (integer) translates to the corresponding ASCII value, and then other types of automatic conversions.

3. For Byte,short,char three types, they are lateral and therefore cannot be automatically converted to each other, and can be cast using the following coercion type. Such as:

Short i=99;
Char c= (char) i;
System.out.println ("Output:" +c);

4. You cannot force a type conversion between a Boolean value and any numeric type;

5. Conversions between different levels of data types can result in a drop in overflow or precision.

6. When the byte-type variable participates in the operation, Java is promoted as an automatic data operation type, converting it to an int type. For example: Byte B;
b=3;
B= (Byte) (b*3);//must declare byte.

Conversion between the wrapper data type and the base data type

A simple type of variable is converted to the appropriate wrapper class, which can take advantage of the wrapper class's constructor. That is: Boolean (boolean value), Character (char value), Integer (int value), long (Long value), float (float value), double (double Value
And in each packing class, the total tangible is xxvalue () method, to obtain its corresponding simple type data. This method can also realize the conversion between different numerical variables, for example, for a double-precision real class, intvalue () can get its corresponding integer variable, and Doublevalue () can get its corresponding double-precision real variable.
1. Conversion between strings and other types
⑴ conversion of other types to strings
① the string conversion method of the Calling class: X.tostring ();
② Automatic conversion: x+ "";
③ method of using String: string.volueof (X);
⑵ a string as a value, to another type of conversion
The ① is converted to the corresponding wrapper instance before the corresponding method is converted to another type.
For example, the character "32.1" converts the value of the double type to the format:

New Float ("32.1"). Doublevalue ().

can also be used: double.valueof ("32.1"). Doublevalue ()
② static Parsexxx method
String s = "1";
byte B = byte.parsebyte (s);
Short T = Short.parseshort (s);
int i = Integer.parseint (s);
Long L = Long.parselong (s);
Float f = float.parsefloat (s);
Double d = double.parsedouble (s);
③character getnumericvalue (char-ch) method

Converting instances

1) Basic type conversion to class type

Forward conversion: A new class-type variable with a class wrapper
Integer a= new Integer (2);
Reverse conversions: Converting through a class wrapper
int B=a.intvalue ();

Eg1:int i=integer.parseint ("123")
Description: This method can only be used to convert a string into an integer variable
EG2:  float f=float.valueof ("123"). Floatvalue ()
Description: The previous example converts a string into a float object and then calls the object's Floatvalue () method returns its corresponding float value.
Eg3: boolean b=boolean.valueof ("123"). Booleanvalue ()
Description: The previous example converts a string into a Boolean object, The Booleanvalue () method of this object is then called to return its corresponding Boolean value.
Eg4:double d=double.valueof ("123"). Doublevalue ()
Description: The previous example converts a string into a double object, and then calls the object's Doublevalue () method returns its corresponding double value.
Eg5: long l=long.valueof ("123"). Longvalue ()
Description: The previous example converts a string into a long object, and then calls the object's Longvalue () method returns its corresponding long value.
Eg6: char=character.valueof ("123"). Charvalue ()
Description: The previous example converts a string into a Character object, and then calls the object's Charvalue () method returns its corresponding char value.

2) Conversion of basic types to strings
Forward conversions:
As follows:
System.out.println ("" +2+3);//"" "" to turn 2 into a string operation;
System.out.println (2+3); There is no conversion.
System.out.println (2+3+ "");//The first two values are added and then "" "is converted to a string.
System.out.println ("+3");//The same as the first one.

The output is displayed as: 23,5,5,23

3) class type to string conversion

Forward conversions: Because each class is a subclass of the object class, and all object classes have a ToString () function, the ToString () function can be converted to
Reverse conversions: A new class-type variable is created from the class wrapper
Eg1:int i=integer.valueof ("123"). Intvalue ()
Note: The previous example converts a string into an integer object, and then calls the object's Intvalue () method to return its corresponding int value.
Eg2:float f=float.valueof ("123"). Floatvalue ()
Note: The previous example converts a string into a float object and then calls the object's Floatvalue () method to return its corresponding float value.
Eg3:boolean b=boolean.valueof ("123"). Booleanvalue ()
Note: The previous example converts a string into a Boolean object and then calls the object's Booleanvalue () method to return its corresponding Boolean value.
Eg4:double d=double.valueof ("123"). Doublevalue ()
Note: The previous example converts a string into a double object, and then calls the object's Doublevalue () method to return its corresponding double value.
Eg5:long l=long.valueof ("123"). Longvalue ()
Note: The previous example converts a string into a long object, and then calls the object's Longvalue () method to return its corresponding long value.
Eg6:char=character.valueof ("123"). Charvalue ()
Note: The previous example converts a string into a character object, and then calls the object's Charvalue () method to return its corresponding char value.

Object-oriented experiment report two

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.