It 18 Palm Seventh Day summary (including job) review--------Object a subclass AA Subclass AAA You can use A AA AAA to create a new subclass You can use either to declare abstract classes and Interfaces------------------- all methods in an interface are abstract abstract classes can have no abstract method Total cannot instantiate internal class post-compilation member location Local location with "$" to interval use numbers to interval anonymous inner class objects------------------------ integrates inheritance of classes, method overrides, and creation of objects into one line of code. format: new The external class name or interface name () {overrides the code in the class or interface, ( You can also customize the content. )} the call to the //function as an example of * button btn1 = new button (); //an anonymous inner class object that inherits the class, overrides the method, and creates an object one go. btn1.addlistener (New buttonlisteneradapter () { // btn1.addlistener is to call the addlistener method the method is to have the listener interface as a parameter, and the listener for {} is equivalent to inheriting &nBsp; public void onclick () { system.out.println ("click once");} This method defines two anonymous class objects worship btn1 and btn2 methods [1]********************************************** When writing code, be careful that the symbol is perfect The parentheses are written in pairs. Use ; end at the end of a statement ***************************************************button btn1 = new button ();//Anonymous inner class object that inherits the class, overrides the method, and creates the object one go. Btn1.addlistener (New buttonlisteneradapter () {Public void onclick () {System.out.println ("click once");}); /Create Btn2 object, use anonymous inner class, give implementation to OnClick (), Ondbclick () two methods. Button btn2 = new button (); Btn2.addlistener (New buttonlisteneradapter () {public Void onclick () {System.out.println ("click once");} Public void ondbclick () {System.out.println ("click two Times");}); [Method 2] Buttonlisteneradapter aa = new buttonlisteneradapter () {public void onclick () {System.out.println ("click once");} Public void ondbclick () {System.out.println ("click two Times");}; //attention to &NBSP;; End two methods result same additional buttonlisteneradapter --> buttonlistener (Object oriented programming) That is Buttonlisteneradapter aa = new buttonlisteneradapter () --> buttonlistener aa = new buttonlisteneradapter () Rewrite local tyrants marriage with anonymous inner class object//define three white, rich, beautiful interface iwhite{ //definition white public void Whiteing (); }interface irich{ //definition of Rich public void riching (); }interface ibeautiful{ //definition Beauty public void Beautifuling (); } //defines an interface that inherits white, rich, beauty three interfaces interface womanstart extends iwhite,irich,ibeautiful{ } //definition local Tyrants class class EarchRicher{ // public&nBsp;void marry (womanstart mm) { system.out.println ("FIND Mating !! "); } } class earchricherman { public static void main (String[] args) { womanstart w = new womanstart () { public void Whiteing () { } public void Riching () { } public void Beautifuling () { } }; earchricher M =new earchricher (); m.marry (W); } } exception ———— ———————————————————————————————— java.lang.throwable The system of java.lang.error java.lang.exception anomaly throwable Error often has a major problem, such as a running class that does not exist or a memory overflow. Do not write a case for code to handle it Exception run at run time, you can pass the try catch The subclass name for Finallyexception and error is the name of the parent class suffix throws used to identify the exception that the function exposes. Throw is used to throw an exception object. The difference between throws and throw: thorws is used on the function, followed by the exception class name. throw is used inside a function, followed by an exception object. Practice Exception Function Declaration class person{private int age; //define function, declare exception public void setage ( Int age) throws exception { //throws Exception declaration if (age < 0 | | &NBSP;AGE&NBSP;>&NBSP;120) {//Throw exception throw new exception ("Age is not legal!!! "); }this.age = age ; // get set Define Age } public int getage () { Return age;}} Class exceptiondemo{public static void main (String[] args) {Person pe = new person (); try{ //try try catch capture handle Exceptions pe.setage ( }catch) (Exception ex) { // catch () What kinds of exceptions are caught Ex.printstacktrace (); //The trace information for the stack that prints the function}}}/*** run * java.lang.exception: age is not legal!!! * at person.setage (Exceptiondemo.java:7) * at exceptiondemo.main (exceptiondemo.java:23) **/package--------------------Package, folder. java/lang/Xxxxjar : java archieve,java archive file. Java/lang/object.class full Name Class name, complete class name : java.lang.object java/lang/object compile time Specify class file to store directory using the-D parameter : The Java -d xx/xxx packagedemo.java runtime specifies the directory where the class file is located using the-CP parameter:java -cp xx/xx A visibility access rule------------------modifier publicprotecteddefaultprivate ---------------------------------------------- --------------------range of similar vvvv VVV sub-class VV different package Vjar-----------------package Java library files. []jar cvf myjar.jar -c classes/ . the class under the Classpath to a jar file] // myjar.jar the file name to be packaged as a custom//classes/is the folder where the class file is located jar cvfe myjar.jar a.a1.a11.a111.a -c &NBSP;CLASSES/&NBSP;.//E the specified entry point. e is the class that contains the main function [use the Java -jar parameter to run the program]java -jar myjar.jar/ /execute JAR file java -jar myjar.jara.a1.a11.a111.a//execute JAR file to specify the entry point. 2. Define the Triangle class Trianle, which contains three int type attributes, representing the length of three edges respectively, &NBSP;&NWhen constructing a triangular object, the sum of any two sides is greater than the third side and, if not, throws a custom exception. Answer:class edgetoosmallexception extends exception { // This exception is used to determine if the side is less than or equal to 0 private string info;public edgetoosmallexception () {}public edgetoosmallexception (String info) {this.info = info;} Public string getinfo () {return info;} Public void setedgeinfo (String info) {this.info = info;}} class edgenotmatchexception extends exception { // This anomaly is used to judge the private of a triangle. String info;public edgenotmatchexception () {}public edgenotmatchexception (String info ) {this.info = info;} Public string getinfo () {return info;} Public void setedgenotmatchexception (String info) {this.info = info;}} class triangle {//three-side private int edgea;private int edgeb;private int Edgec;// edgea's Get/set method public int getedgea () {return edgea;} Public void setedgea (Int edgea) throws EdgeTooSmallException {if (Edgea > 0) {this.edgea = edgea;} else {throw new edgetoosmallexception ("Edge length a cannot be less than or equal to 0");}} edgeb Get/set Method Public int getedgeb () {return edgeb;} Public void setedgeb (INT&NBSP;EDGEB) throws EdgeTooSmallException {if (Edgeb > 0) {this.edgeb = edgeb;} else {throw new edgetoosmallexception ("Edge length B cannot be less than or equal to 0");}} edgec Get/set Method Public int getedgec () {return edgec;} Public void setedgec (INT&NBSP;EDGEC) throws EdgeTooSmallException {if (EDGEC > 0) {this.edgec = edgec;} else {throw new edgetoosmallexception ("Edge length C cannot be less than or equal to 0");}} Judging is not triangular public static void checking (int x, inT y, int z) throws edgenotmatchexception {if (x + y <= z | | x + z <= y | | y + z <= x) {throw new edgenotmatchexception ("This is not a triangle");} else {system.out.println ("This is a triangle");}}} Class triangledemo {public static void main (String[] args) {// Construct the Triangle Triangle t1 = new triangle ();// set three edges, find the anomaly Try {t1.setedgea (4); T1.setEdgeb (5 ); T1.setedgec (3); T1. Checking (T1.getedgea (), t1.getedgeb (), &NBSP;T1.GETEDGEC ()); catch (edgetoosmallexception e) {system.out.println (E.getinfo ()); System.exit (-1);} catch (edgenotmatchexception e) {system.out.println (E.getinfo ()); System.exit (-1);} System.out.println ("Side length for" + t1.getedgea () + "," + t1.getedgeb () + "," + &NBSP;T1.GETEDGEC ());}} The birthday attribute is added to the 3.Person class for Setbirthday (INT&NBSP;,INT&NBsp;, int ) methods for exception handling, requirements are valid for the year, the month is valid, the date is valid, the specified date is valid, and the different exceptions are thrown separately. A: Add the birthday attribute to the/** * person class and treat the Setbirthday (int ,int , int ) method with exception, * The required year is valid, the month is valid, the date is valid, the date specified is valid, and different exceptions are thrown separately. * */public class Brithday {private String birthday;public String getbirthday () {return birthday;} Public void setbirthday (int year, int month, int date) throws invalidparamexcetion {// judgment Year if (year < 1900 | | year > 2016) {throw new invalidparamexcetion ("Year is inappropriate, please pass the year 1900 to 2016"); Judging month if (month <= 0 | | &NBSP;MONTH&NBSP;>&NBSP;12) {throw new invalidparamexcetion ("The month is inappropriate, does not exist" + month + "Month");} Judgment Date if (date > 31 | | date <= 0) {throw new invalidparamexcetion ("date inappropriate, not present" + date + " Day ");} boolean isthirdone = date == 1 | | date == 3 | | date == 5 | | date == 7| | date == 8 | | date == 10 | | date == 12;if (!isthirdone && date == 31) {throw New invalidparamexcetion ("date not appropriate," + month + "month does not exist" + date+ "Day");} if (month == 2 && date > 29) {throw new Invalidparamexcetion ("date is inappropriate, February does not exist" + date + "Day");} if (year % 4 != 0 && month == 2 && date == 29) {throw new invalidparamexcetion ("date inappropriate," + year + "is not a leap year, So February does not exist "+ date + " Day ");} System.out.println ("Birthday is:" + year + "year" + month + "Month &NBSP;&N"BSP; " + date + "Day ");} Public static void main (String[] args) throws invalidparamexcetion { Brithday person = new brithday (); Try {person.setbirthday (2015, 12, 5); Person.setbirthday (2016, 2, 29);p erson.setbirthday (2015, 2, 29);// because the last sentence was abnormal, So will not execute this sentence person.setbirthday (2015, 3, 5);} catch (invalidparamexcetion e) {}}}/** * illegal parameter exceptions * */class Invalidparamexcetion extends exception {public invalidparamexcetion (String msg) {Super (MSG); SYSTEM.OUT.PRINTLN (msg);}} 4. Define the class to be under the specified package. Com.it18zhang, after compiling, make a jar file. A: Package Java library files. []jar cvf myjar.jar -c classes/ .jar cvfe myjar.jar the class under the Classpath to a jar file] COM.IT18ZHANG.A&NBSP;-C&NBSP;CLASSES/&NBSP;.//E the specified entry point. [Use the Java -jar parameter to run the program]java -jar myjar.jar//execute the jar file java -jar myjar.jarcom.it18zhang.a// Executes the jar file to specify the entry point. 5Use the jar package with each other and place the CP to reuse the class. A: The answer is slightly 6. Design the program and examine the modifiers. Public -> protected -> default -> private (optional) answer:package com.it18zhang.job.demo;public class person {protected string protectname;public Void method_public () {system.out.println ("Person method_public");} Protected void method_protect () {System.out.println ("Person method_protect");} Void method_defult () {System.out.println ("Person method_defult");} Private void method_private () {System.out.println ("person method_private");}} Package com.it18zhang.job.demo;public class people {public static void main (String[] args) {system.out.println ("class modifier permission test in the same package"); Person person = new person ();p erson.method_public ();p erson.method_protect ();p erson.method_ Defult ();//Cannot call Private//person.method_private ();}} Package com.it18zhang.job.demo2;import com.it18zhang.job.demo.Person;public class student extends person{public static void main (String[] args) {system.out.println ("Subclass modifier permission Test"); Student student = new student (); Student.method_public (); Student.method_protect ();// Unable to invoke Defult, Private//student.method_private ();//student.method_defult ();}} Package com.it18zhang.job.demo2;import com.it18zhang.job.demo.person;public class other {public static void main (String[] args) {system.out.println ("class modifier permission test in other packages"); Person person = new person ();p erson.method_public ();//cannot invoke Protect,default and private methods of classes in other packages/ /person.method_protect ();//person.method_defult ();//person.method_private ();}}
It 18 Palm Seventh Day summary (including jobs)