我试图了解如何super在python中使用
class people:
name = ''
age = 0
__weight = 0
def __init__(self,n,a,w):
self.name = n
self.age = a
self.__weight = w
def speak(self):
print("%s is speaking: I am %d years old" %(self.name,self.age))
class student(people):
grade = ''
def __init__(self,n,a,w,g):
#people.__init__(self,n,a,w)
super(student,self).__init__(self,n,a,w)
self.grade = g
def speak(self):
print("%s is speaking: I am %d years old,and I am in grade %d"%(self.name,self.age,self.grade))
s = student('ken',20,60,3)
s.speak()
Run Code Online (Sandbox Code Playgroud)
上面的代码出现以下错误:
---------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-147-9da355910141> in <module>()
10
11 …Run Code Online (Sandbox Code Playgroud) 为什么(T)这个通用接口需要不安全的转换?如果T与自身相当,也就是ExtendedComparable<super of T>那些也意味着ExtendedComparable<T>,那么为什么需要ExtendedComparable<T>将类型擦除转换为T?
/* @param <T> T must be comparable to itself or any of its superclass
* (comparables are consumers, thus acc. to the PECS principle
* = producer-extends,consumer-super we use the bounded wildcard type "super")
*/
public interface ExtendedComparable<T extends ExtendedComparable<? super T>> {
Comparator<? super T> getComparator();
default boolean greaterThen(T toCompare) {
return getComparator().compare((T) this, toCompare) > 0;
}
}
Run Code Online (Sandbox Code Playgroud) 我想更改工作表中单元格中填充的确定数据的字体大小。假设该单元格是第 26 列的第 2 行。以范围方式来说,这将是:Z2。字体大小为11。例如,我想将字体大小更改为72。
我怎样才能做到这一点?
是否有在 1 个 if 语句中包含两个海象运算符的正确方法?
if (three:= i%3==0) and (five:= i%5 ==0):
arr.append("FizzBuzz")
elif three:
arr.append("Fizz")
elif five:
arr.append("Buzz")
else:
arr.append(str(i-1))
Run Code Online (Sandbox Code Playgroud)
该示例适用于three但five将“未定义”。
warning: [unchecked] unchecked call to setCellValueFactory(Callback<CellDataFeatures<S,T>,ObservableValue<T>>) as a member of the raw type TableColumn column1.setCellValueFactory(new PropertyValueFactory<String, State>("name")); where S,T are type-variables:
S extends Object declared in class TableColumn
T extends Object declared in class TableColumn
Run Code Online (Sandbox Code Playgroud)
码:
column1.setCellValueFactory(new PropertyValueFactory<>("name"));
Run Code Online (Sandbox Code Playgroud)
warning: [unchecked] unchecked call to add(E) as a member of the raw type List
transitionTable.getColumns().add(column1);
where E is a type-variable:
E extends Object declared in interface List
Run Code Online (Sandbox Code Playgroud)
码:
transitionTable.getColumns().add(column1);
Run Code Online (Sandbox Code Playgroud)
warning: [unchecked] unchecked call to setAll(Collection<? extends E>) as a member of the …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 python3 中编写一个简单的递归函数。在学习 OO Java 时,我也想编写涉及对象的 Python 代码。下面是我的代码。我提示用户输入一个数字,屏幕应该显示每个小于 5 的整数。
class Recursion:
@staticmethod
def recursive(x):
if (x>5):
print (x)
recursive(x - 1)
def main(self):
x = int(input('Enter a number for recursive addition: '))
recursive(x)
Run Code Online (Sandbox Code Playgroud)
但是,当我在终端上运行它时,它会显示:“NameError: name 'recursive' is not defined”。这是错误的样子:
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 5 2015, 21:12:44)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from Recursion import *
>>> a = Recursion()
>>> a.main()
Enter a number …Run Code Online (Sandbox Code Playgroud) 我正在使用Python 2.x,并尝试使用命名参数来理解字符串格式的逻辑.我明白:
"{} and {}".format(10, 20)打印'10 and 20'.
以类似的方式'{name} and {state}'.format(name='X', state='Y')打印X and Y
但为什么这不起作用?
my_string = "Hi! My name is {name}. I live in {state}"
my_string.format(name='Xi', state='Xo')
print(my_string)
Run Code Online (Sandbox Code Playgroud)
它打印 "Hi! My name is {name}. I live in {state}"
我们可以对正在运行的线程进行弱引用,以便在低性能CPU中终止吗?
我的意思是,做这样的工作吗?
WeakReference<Thread> ref = new WeakReference<Thread>(new Thread(){
public void run(){
while(true){ /* your code */ }
}
});
Run Code Online (Sandbox Code Playgroud)
这样,我想要一个线程具有低优先级来执行,我的意思是当CPU性能为低并且CPU完全使用时,线程的执行会自动终止.
有些线程不具有高优先级,应该以低CPU性能中断和终止.
我在Java spring MVC应用程序中将Hibernate Query Language(HQL)与Oracle数据库一起使用。我以这种方式编写了一个HQL更新查询:
String hql = "update " + MyModel.class.getName() + " e set e.field = " + value + " where ..."
//Acquiring session
...
Query query = session.createQuery(hql);
int result = query.executeUpdate();
Run Code Online (Sandbox Code Playgroud)
该executeUpdate()方法返回更新的行数。但是我想在执行更新查询后获取更新行的ID的列表。HQL有什么方法可以做到这一点吗?
尽管变量current_timestamp(因此也是函数返回值)和变量time是datetime.datetime类对象,但以下代码引发 AttributeError: type object 'datetime.datetime' has no attribute 'datetime'
from datetime import datetime
def current_time() -> datetime.datetime:
current_timestamp: datetime.datetime = datetime.now()
return current_timestamp
time: datetime.datetime = current_time()
print(time)
Run Code Online (Sandbox Code Playgroud)
仅更改函数返回值和time变量的注释即可解决问题。
from datetime import datetime
def current_time() -> datetime:
current_timestamp: datetime.datetime = datetime.now()
return current_timestamp
time: datetime = current_time()
print(time)
Run Code Online (Sandbox Code Playgroud)
谁能解释为什么注释datetime.datetime只为函数返回值和time变量引发 AttributeError而不是为current_timestamp变量引发?
我正在运行 Python 3.8。
java ×5
python ×5
python-3.x ×4
generics ×2
python-2.7 ×2
apache-poi ×1
casting ×1
hibernate ×1
hql ×1
javafx ×1
nameerror ×1
oop ×1
oracle ×1
recursion ×1
subprocess ×1
type-erasure ×1
unchecked ×1
warnings ×1