classes cannot be accessed directlySYSTEM.OUT.PRINTLN (name);//only static members of external classes can be accessed directlySystem.out.println ("Inner" +address);//Access this internal class member. } } Public voidPrintinfo () { person person=NewPerson (); Person.display (); //System.out.println (mail);//not accessible//System.out.println (address);//not accessibleSystem.out
Java is an object-oriented language, Java object-oriented generally have three major characteristics: encapsulation, inheritance, polymorphism.Encapsulation: It is the encapsulation of properties and methods into a class.Inheritance: As subclasses inherit some properties and methods of the parent class.Polymorphic: Just as a parent class has multiple subclasses of different characteristics.Here I do not exp
The second article in objective Java yesterday: when encountering multiple constructor parameters, consider using constructor. In this example, the builder mode is used to build complex objects. This is because I was not familiar with Java. Q: Can I build an instance with static class? Previously, static classes in. Net had never been instantiated.
After checkin
Obscurity also have a career, and know is also boundless. ——— "Chuang Tzu"In the previous article, we already know the principles and procedures for designing a thread-safe class, as well as the details that we should be aware of during the design process. In fact, the Java collection library contains a thread-safe collection and a non-thread-safe collection, and it can be said that the Java thread-Safe col
In the Java class system, there are two more special abstract bodies-abstract classes and interfaces. Abstract bodies cannot have specific objects (and interfaces are not even classes), but they are two very important concepts in the Java design world, and many good design patterns are based on these two concepts, alth
implement the definition class's inner, you can temporarily create an instance object of the anonymous subclass of Class A when passing parameters to the Callinner method, the code is as follows:Source: Outer.javaClasses Aabstract class a{abstract void display ();//declares an abstract class}//external classes public class Outer {//main member method public static void main (Strin G[] (args) { new Outer (). Callinner (New A () { void display () {
, we first have to create an object of the outer class, and then we can create an object of the inner class, and the inner class gets the field (fields) and method (methods) of the outer class directly. Create inner classes in the following ways:OuterClass.InnerClass innerObject = outerObject.new InnerClass();An inner class cannot define a static variable (except static final) because it relies on an object of an external class.So, the difference betw
implement all abstract methods in the abstract class, or the subclass must be an abstract class. The subclass implementation interface must implement all the abstract methods in the interface.
Case 2: Presentation of the interfacepublic Class Interfacedemo {public static void main (string[] args) {//inter6 o1= new Inter6 (); Compile error, interface cannot be instantiated Inter6 O2 = new Moo (); Up styling Inter5 O3 = new Moo ();}} Demo interface Syntax interface inter1{public static fi
" + address);//Access this inner class member. }} public void Printinfo () {Person person = new person (); An external class accesses a non-static member of an inner class: instantiates an inner class to Person.display (); System.out.println (mail)///unreachable//SYSTEM.OUT.PRINTLN (address)//unreachable System.out.println (person.address); You can access the private member of the Inner class System.out.println (person.x);//The external class accesses the static members of the Inne
() {System.out.println ("---2---animal eat"); } Public void Sleep() {System.out.println ("---2---animal sleep"); } }}//Create a new test class Public class Test { Public Static void Main(string[] args) {Zoo z =NewZoo (); Animal an = Z.getanimal1 (); An.eat (); An.sleep (); Animal one = Z.getanimal2 (); One.eat (); One.sleep (); }}Anonymous inner classes are a simplified way of writing internal classes:r
One: wrapper classes for basic data typesThe Java language provides the corresponding wrapper class for all basic data typesBasic data Type wrapper classByte (bytes) java.lang.Bytechar (character) Java.lang.CharacterShort (shorter integer) java.lang.Shortint (integral type) Java.lang.IntegerLong (L-integer) Java.lang.LongFloat (float type) java.lang.FloatDouble (dual precision) java.lang.DoubleBoolean (Bool
publicvoidcalculate () {switch (option) {case ' + ': System.out.println ("Do the addition operation, the result is:" + (num1+ num2));break;case '-': System.out.println ("Do subtraction, the result is:" + (NUM1-NUM2));break;case ' * ': System.out.println ("Do the multiplication, the result is:" + (NUM1*NUM2));break;case '/': System.out.println ("Do the division operation, the result is:" + (num1/num2)); Classdemo6{publicstaticvoidmain (String[]args) {//created a Calculator object Calculatorc=new
Java Study Notes 20 --- adding internal classes to member Internal classes (1), Study Notes 20 ---
In the previous article, note 19 briefly introduced the internal class, local internal class, and anonymous internal class of the member, and added some content to the internal class of the member.
There are mainly the following six points:
1. The member's internal
System.out.println("Inner "+address);//访问本内部类成员。 } } public void printInfo(){ Person person = new Person(); person.display(); //System.out.println(mail);//不可访问 //System.out.println(address);//不可访问 System.out.println(person.address);//可以访问内部类的私有成员 System.out.println(person.mail);//可以访问内部类的公有成员 } public static void main(String[] args) { StaticTest staticTest = new StaticTest(); staticTest.printInfo(); } }
Within a static nested class, non-static members of the outer class cannot be accessed, as
anonymous class will have a member variable that points to a reference to an external class2. If an anonymous class is to use one of the parameters in the method, the corresponding parameter of the method must be final, which seems to be mandatory for Java. and a member variable in an anonymous class points to the same storage area that the parameter object points to3. Anonymous class access to members of an external class is accessed through a stati
inheritance is from top to bottom, Grandpa 1, Dad 1, Son 1, Grandpa 2, dad 2, son 2, not: grandpa 2, Son 1, is a mess like this. No, it's a joke. Therefore, class rules do not inherit the relationship, prevent chaos, and make the structure clear.Therefore, when we use a tree structure, we use classes.
There are still many such situations in our lives. For example, if Michael is a Chinese, Michael is also an American dual identity.Another example is
Get started with java-8.9 Nested classes (using static internal classes)
In this section, we will discuss Nested classes.
1. Concept
When internal classes use static, they are nested classes.
package com.ray.ch07;public class Tes
obj.test (); at } - } - /* - F:\java_example>java Testouter - The out_i is*/View CodeFirst call this display () function to print out the out_i, it first in the display () function to find whether the variable, not to the inner class to find, and finally to outer class to find the variable2, the use of internal classesInternal classes make program code more compact and modular.When the program code in
Interfaces can inherit Interfaces. Abstract classes can implement (implements) interfaces, and abstract classes can inherit concrete Classes. There can be static main methods in an abstract class.Q: Does an abstract class inherit entity classes (concrete Class)A: An abstract class can inherit an entity class, but only
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.