java inner class

博客围绕Java展开,涉及class、object等内容,可能探讨了Java类和对象的使用,以及编译器在处理相关代码时的机制等信息技术领域关键信息。
2004-3-21 星期日(Sunday) 小雨

克隆方法
如果这个类是可以被克隆的,那么下一步就是 clone 方法:

public
Object clone() {
 try {
 CounterSet obj = (CounterSet)super.clone();
 obj.packets = (int[])packets.clone();
 obj.size = size;
 return obj;
 }catch(CloneNotSupportedException e) {
 throw new InternalError("Unexpected CloneNotSUpportedException: " + e.getMessage());
 }
}


toString 方法
无论如何,每一个类都应该定义 toString 方法:

public
String toString() {
 String retval = "CounterSet: ";
 for (int i = 0; i < data.length(); i++) {
 retval += data.bytes.toString();
 retval += data.packets.toString();
 }
 return retval;
 }
}


不必要的对象构造
不要在循环中构造和释放对象


使用 StringBuffer 对象
在处理 String 的时候要尽量使用 StringBuffer 类,StringBuffer 类是构成 String 类的基础。String 类将 StringBuffer 类封装了起来,(以花费更多时间为代价)为开发人员提供了一个安全的接口。当我们在构造字符串的时候,我们应该用 StringBuffer 来实现大部分的工作,当工作完成后将 StringBuffer 对象再转换为需要的 String 对象。比如:如果有一个字符串必须不断地在其后添加许多字符来完成构造,那么我们应该使用 StringBuffer 对象和她的 append() 方法。如果我们用 String 对象代替 StringBuffer 对象的话,会花费许多不必要的创建和释放对象的 CPU 时间。


Note three items in CloneDemo1. First, the throws CloneNotSupportedException clause attached to main()'s signature means you wish to ignore any CloneNotSupportedException objects thrown from clone(). Those exception objects are thrown in one of two situations -- either you overrode clone() and purposely threw the exception in a subclass (to prevent subclass objects from being cloned) or you called Object's clone() method from code in a class that does not also implement the Cloneable interface.

As you can probably guess, the second item to note is the implements Cloneable clause. Cloneable is an interface -- a concept I will discuss in next month's article. For now, keep in mind that an interface resembles a class, but can only declare method signatures and constants. If you examine the documentation on Cloneable, you discover that the interface is empty. You must specify implements Cloneable in the class declaration for those classes whose code calls Object's clone() method. Behind the scenes, Cloneable helps Object's clone() method identify those subclasses that must have their field values duplicated. (Object's clone() method throws a CloneNotSupportedException object if a class, whose code calls that method, does not implement Cloneable.)

The final item to note is the comment line in CloneDemo1. That line attempts to clone an AnotherClass object. I inserted that comment because the line of code won't compile. By default, you cannot clone an object from another class. To do so, you must first override clone(), as Listing 6 demonstrates:


If you want to make an object of the inner class anywhere except from within a non-static method of the outer class, you must specify the type of that object as OuterClassName.InnerClassName, as seen in main( ).


OuterClass oc = new OuterClass();
OuterClass.InnerClass ic = oc.getInnerClass();
ic.showMe();
OuterClass.InnerClass ic2 = oc.new InnerClass();
ic2.showMe();

Thus, the private inner class provides a way for the class designer to completely prevent any type-coding dependencies and to completely hide details about implementation. In addition, extension of an interface is useless from the client programmer’s perspective since the client programmer cannot access any additional methods that aren’t part of the public interface. This also provides an opportunity for the Java compiler to generate more efficient code.

Normal (non-inner) classes cannot be made private or protected; they may only be given public or package access.

If you’re defining an anonymous inner class and want to use an object that’s defined outside the anonymous inner class, the compiler requires that the argument reference be final, like the argument to dest( ). If you forget, you’ll get a compile-time error message. Feedback

In this case, the variable i did not have to be final. While i is passed to the base constructor of the anonymous class, it is never directly used inside the anonymous class. Feedback


Here’s the “parcel” theme with instance initialization. Note that the arguments to dest( ) must be final since they are used within the anonymous class:

So an inner class has automatic access to the members of the enclosing class. How can this happen? The inner class must keep a reference to the particular object of the enclosing class that was responsible for creating it. Then, when you refer to a member of the enclosing class, that (hidden) reference is used to select that member. Fortunately, the compiler takes care of all these details for you, but you can also understand now that an object of an inner class can be created only in association with an object of the enclosing class. Construction of the inner class object requires the reference to the object of the enclosing class, and the compiler will complain if it cannot access that reference. Most of the time this occurs without any intervention on the part of the programmer.


If you don’t need a connection between the inner class object and the outer class object, then you can make the inner class static. This is commonly called a nested class.[36] To understand the meaning of static when applied to inner classes, you must remember that the object of an ordinary inner class implicitly keeps a reference to the object of the enclosing class that created it. This is not true, however, when you say an inner class is static. A nested class means: Feedback


You don’t need an outer-class object in order to create an object of a nested class. Feedback
You can’t access a non-static outer-class object from an object of a nested class. Feedback

 However, if you make a nested class (a static inner class), then it doesn’t need a reference to the outer class object


Each inner class can independently inherit from an implementation. Thus, the inner class is not limited by whether the outer class is already inheriting from an implementation.

why inner class?
Without the ability that inner classes provide to inherit—in effect—from more than one concrete or abstract class, some design and programming problems would be intractable. So one way to look at the inner class is as the rest of the solution of the multiple-inheritance problem. Interfaces solve part of the problem, but inner classes effectively allow “multiple implementation inheritance.” That is, inner classes effectively allow you to inherit from more than one non-interface. Feedback
Java inner join 是一种用于关系型数据库的查询操作,用于从两个或多个表中返回有关联的行。 Inner join(内连接)是在两个表之间根据相关字段(通常是外键)的匹配来连接它们的行。只有当两个表中的相关字段具有相同的值时,相应的行才会被连接。连接的结果是一个新的表,其中包含了两个表中相关字段匹配的行。 Java中实现inner join的方法有多种,最常用的方法是使用JDBC(Java Database Connectivity)库来连接数据库并执行查询操作。在JDBC中,可以使用SQL语句来实现inner join。下面是一个示例代码: ```java import java.sql.*; public class InnerJoinExample { public static void main(String[] args) { try { // 连接数据库 Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password"); // 创建并执行inner join查询 String query = "SELECT table1.column1, table2.column2 FROM table1 INNER JOIN table2 ON table1.column3 = table2.column4"; Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(query); // 处理查询结果 while (resultSet.next()) { String column1Value = resultSet.getString("column1"); String column2Value = resultSet.getString("column2"); System.out.println(column1Value + " " + column2Value); } // 关闭连接 resultSet.close(); statement.close(); connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } ``` 以上代码演示了如何使用Java和JDBC实现inner join查询。需要注意的是,具体的SQL查询语句需要根据实际的数据库表结构和字段名来编写。 Java inner join 可以帮助我们从关联的表中获取需要的数据,提供了一种灵活、可靠的方法来处理复杂的数据查询需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值